Python threading tutorial?

hg hansgeunsmeyer at
Sun Jan 5 19:43:19 CET 2003

Gerrit Holl <gerrit at> wrote in message 

> 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([newx][newy], self.__class__):
> 			raise Collision
>[self.x][self.y] =
>[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 Or you have to add some locking mechanism to function move.


More information about the Python-list mailing list