On Tue, May 29, 2012 at 10:51 AM, Bernhard <
boe...@gmail.com> wrote:
> Hello together,
> i have a dict that holds all channels for my chat application. If a user
> joins a channel, i check if this channel exists in my dict, if not =>
> create.
>
> [...]
> channels = dict()
> def join_user(request, channel)
> if channel not in self.channels:
> self.channels[channel] = set()
> self.channels[channel].add(request)
>
> I wonder who to get this thread safe in tornado. Locking seems not be be a
> good solution under high load?
Why not? If you need a lock, use a lock. You can sometimes take
advantage of atomic dict operations, but those operations are made
atomic via the GIL so you're not avoiding any of the fundamental
issues that can arise with locks (and the GIL ensures that contention
on your own fine-grained locks is low so they're pretty cheap). I
recommend not relying too much on atomic dict operations since they
are so subtle (is the key's __hash__ method defined in C or python?
Does defaultdict have the same atomicity properties as a regular dict?
etc...).
Of course all of this may be moot since as Claudio pointed out Tornado
doesn't create any threads, so threading isn't an issue unless you
create the threads yourself.
-Ben