Python threading tutorial?
exarkun at intarweb.us
Sun Jan 5 19:25:48 CET 2003
On Sun, Jan 05, 2003 at 10:03:38AM +0100, Gerrit Holl wrote:
> Dennis Lee Bieber schreef op zondag 5 januari om 01:30:38 +0000:
> > [snip]
> > On collision, that thread would have to message the other
> > thread to do the interaction. Sounds like an awful lot of message
> > Queues (see Queue.Queue) and maybe synchronization primitives (see
> > threading.Semaphore, threading.Event, threading.Condition).
> ...so I am currently not using the queues. Does that mean my application
> is not reliable, or is this a reliable solution? It seems to run ok...
That's the problem with threads -- they seem to work for a while, even
when you're using them wrong.
> My source code:
> [big snip]
> if isinstance(self.mx[newx][newy], self.__class__):
> raise Collision
> self.mx[self.x][self.y] = self.mx.bg
> self.mx[newx][newy] = self
> self.x = newx
> self.y = newy
Above, you have a race condition. If thread A and thread B are both
moving to the same matrix location and thread A runs the first two lines,
then thread B runs the whole block, when thread A resumes, it will believe
the destination to be unoccupied and happily trample all over thread B. It
probably seems to work right because you haven't tested the system under a
high load yet.
If it jams -- force it. If it breaks, it needed replacing anyway.
12:00am up 20 days, 9:46, 2 users, load average: 0.10, 0.14, 0.10
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 196 bytes
Desc: not available
More information about the Python-list