[Baypiggies] Synchronized in Python

Aahz aahz at pythoncraft.com
Mon Dec 3 15:57:09 CET 2007


On Mon, Dec 03, 2007, Shannon -jj Behrens wrote:
> On Nov 25, 2007 8:47 AM, Aahz <aahz at pythoncraft.com> wrote:
>> On Sun, Nov 11, 2007, Monte Davidoff wrote:
>>>
>>> Stephen McInerney and I were talking after the last BayPIGgies meeting
>>> (Concurrency in Python) and thought it might be of help to post how to
>>> get the effect of the Java synchronized keyword in Python.  (I arrived a
>>> little late, so perhaps this was mentioned before I got there.)  This
>>> post is meant as a quick example, so the explanation is brief and I hope
>>> not too confusing.  In Java, the synchronized keyword ensures that only
>>> one thread is executing a method or block at a time.
>>
>> Generally speaking, my recommendation is that you ensure that only one
>> thread has access to an object at any time.  It's much simpler.  Just use
>> a queue to pass objects between threads.  You can find examples in my
>> thread tutorial slides.
> 
> I agree.  That's my favorite approach as well.
> 
> /me giggles:
> I also like passing function references via the queue as well.  It's
> like, "Here, call this function, but call it on your thread." ;)

That's a perfectly sensible way of managing callbacks.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith


More information about the Baypiggies mailing list