global threading.Lock not locking correctly?

Afanasiy abelikov72 at hotmail.com
Tue Feb 4 09:57:03 EST 2003


# My logic tells me this should never print 'OOPS', yet it does.
# Can someone tell me why? (P.S. this is simplified example code)

import threading, time

class testthread(threading.Thread):
  
  def run(self):    
    for x in range(40):
      
      global lock
      global visitors
      
      lock.acquire      
      
      visitors += 1
      if visitors > 1:
        print 'OOPS!'+str(visitors),
      else:
        print '.',
      visitors -= 1
      
      lock.release

      #time.sleep(0.01) #less OOPS of course
    
visitors = 0
lock = threading.Lock()

for i in range(10):
  testthread().start()





More information about the Python-list mailing list