Skip to content

Instantly share code, notes, and snippets.

@copitux
Created February 27, 2012 17:46
Show Gist options
  • Save copitux/1925816 to your computer and use it in GitHub Desktop.
Save copitux/1925816 to your computer and use it in GitHub Desktop.
modbus tk sample
#!/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