Skip to content

Instantly share code, notes, and snippets.

@taojy123
Created November 10, 2017 03:04
Show Gist options
  • Save taojy123/6701ae198e21b13fa4a66273f33ceebd to your computer and use it in GitHub Desktop.
Save taojy123/6701ae198e21b13fa4a66273f33ceebd to your computer and use it in GitHub Desktop.
线程锁装饰器
import thread
import time
lock = thread.allocate_lock()
def mutex(func):
def wrapper(*arg, **kwargs):
global lock
lock.acquire()
r = func(*arg, **kwargs)
lock.release()
return r
return wrapper
@mutex
def worker(t):
print 'worker begin', t
time.sleep(t)
print 'worker finish', t
thread.start_new_thread(worker, (5, ))
thread.start_new_thread(worker, (4, ))
thread.start_new_thread(worker, (3, ))
while True:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment