Python threading tutorial?

Gerrit Holl gerrit at nl.linux.org
Sun Jan 5 16:43:00 EST 2003


Hi,

Dennis, JP and hg, thanks for your answers,

Gerrit wrote:
> > ...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...

hg wrote:
> It's definitely not reliably with your Matrix class, and I'm surprised
> you haven't noticed any difficulties yet.


Hmm, I was actually not testing it. Now that I have added the following
code...:

if self.mx[self.x][self.y] is not self:
	raise "bezet...", "%s %s" % (self, self.mx[self.x][self.y])

...I see it is indeed wrong.

> > 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.

I see. The threading tutorial by Aahz Maruch is not an ideal tutorial to
read through the web, but page 33 helps me; it says:

mutex.acquire()
<dangerous code>
mutex.release()

confusingly, those acquire() and release() functions are not in the mutex
module but Lock() methods; I need to dive deeper in this matter, to better
understand this Lock()ing... hmm... my code seems to hang randomly when
adding this... hmm... I'll try and work this out and will probably come with
a pointed question later this week... hmm... studying... hmm... hmm..

hmm-ing yours,
Gerrit.

-- 
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list