Created
February 27, 2012 17:46
-
-
Save copitux/1925816 to your computer and use it in GitHub Desktop.
modbus tk sample
This file contains 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
#!/usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
import threading | |
import time | |
from random import random | |
lock = threading.Lock() | |
def thread_safe(func): | |
def wrapper(*args, **kwargs): | |
with lock: | |
func(*args, **kwargs) | |
return wrapper | |
@thread_safe | |
def execute(tid): | |
working_time = random() | |
time.sleep(working_time) | |
print 'Th[%s]: Working %s sec' % (tid, working_time) | |
for i in xrange(60): | |
threading.Thread(target=execute, args=(i,)).start() | |
print 'exit before ends...' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment