Python threading tutorial?
hansgeunsmeyer at earthlink.net
Sun Jan 5 19:43:19 CET 2003
Gerrit Holl <gerrit at nl.linux.org> wrote in message
> ...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...
It's definitely not reliably with your Matrix class, and I'm surprised
you haven't noticed any difficulties yet.
> My source code:
> class Person(threading.Thread):
> def move(self, dir):
> 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
For instance, if there is a thread switch immediately after isinstance
the state of your "world" may become inconsistent:
- thread A: person A sees that (newx, newy) is empty
- thread-switch to B: person B sees that (newx, newy) is empty, and
occupies those coordinates
- thread-switch back to A: person A occupies the same coordinates...
So, in class Matrix you have to add some kind of locking (see
Queue.py). Or you have to add some locking mechanism to function move.
More information about the Python-list