[Tutor] locks and threads
kent37 at tds.net
Sun Aug 3 05:38:50 CEST 2008
On Sat, Aug 2, 2008 at 2:36 PM, James <jtp at nc.rr.com> wrote:
> I'm trying to write a class that will acquire a lock before entering a
> critical section, and then release it. Does this look like the right
> way to go about accomplishing my goal?
> grabLock = self.lock.acquire( 0 )
> if grabLock:
> print 'acquired lock successfully'
> print "did *not* obtain lock"
> < ** what do I put here? ** >
> if grabLock is True:
> <do something "critical" here>
> print 'released lock'
> What should I be doing in the else: statement? If I can't grab the
> lock, should I simply try again? Maybe a while loop that keeps trying
> until I grab the lock? (I'm not really sure how I'm going to integrate
> the 'try' statement with the while loop, though, to solve the problem
> of not grabbing the lock)
What are you using for the lock. A threading.Lock will block if you
try to acquire it and it is not available.
Why do you need the try? You could put the acquire in a while loop.
You probably want to sleep in the loop, so you don't chew up too much
More information about the Tutor