Threading blocking functions
Duncan Booth
duncan at rcp.co.uk
Tue Aug 22 06:01:04 EDT 2000
piet at cs.uu.nl wrote in <usnryubiy.fsf at cs.uu.nl>:
>>>>> Roy Katz <katz at Glue.umd.edu> (RK) writes:
>RK> If a function is called simulatenously by different threads,
>RK> will each thread blocand wait for the function to finish executing,
>RK> or will a new copy of the function begin executing? How can I specify
>RK> either behaviour (blocking, nonblocking)?
>
>They may execute simultaneously. If you don't want that you have to set a
>lock.
>
>
It may be of use to know that Zope includes a C extension module called
'Sync' that provides a mixin class 'Synchronized'. You can use this module
in non-Zope python programs quite easily (just make sure that Sync.pyd is
somewhere on your PythonPath and then import Sync).
Basically, any class that includes Sync.Synchronized as a base class will
automatically single thread calls to all of its member functions. So the
original example:
RK> def update( data ):
RK> do-something-with-data
could be implemented as:
import Sync
class updateclass(Sync.Synchronized):
def update(self, data):
do-something-with-data
update = updateclass().update
Every instance creates its own lock and stores it in the _sync__lock member
of the class.
More information about the Python-list
mailing list