Created
November 10, 2017 03:04
-
-
Save taojy123/6701ae198e21b13fa4a66273f33ceebd to your computer and use it in GitHub Desktop.
线程锁装饰器
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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