Threading blocking functions

piet at cs.uu.nl piet at cs.uu.nl
Mon Aug 21 06:57:41 EDT 2000


>>>>> Roy Katz <katz at Glue.umd.edu> (RK) writes:

RK> Hi!
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.

RK> I wrote a piece of code with the following behaviour:

RK> # only one copy of this function must run at one time.
RK> def update( data ):
RK>    do-something-with-data

RK> # many of these are run, and they all call update(). 
RK> def thread():
RK>    # I want this to block until update() is no longer busy
RK>    # servicing other threads.
RK>    data = something
RK>    update( data )

For example, the threading class has Lock ():

updatelock = Lock()
This must be done ONCE in a central location, and each thread must have
access to it.

Then in each thread:
updatelock.aquire()
update(data)
updatelock.release()

Alternatively, you can put aquire() as the first statement in update, end
release() as the last.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl



More information about the Python-list mailing list