Basic synchronization decorator,,下面代码是对python
Basic synchronization decorator,,下面代码是对python
下面代码是对python同步的简单封装
def synchronized(lock): """ Synchronization decorator. """ def wrap(f): def newFunction(*args, **kw): lock.acquire() try: return f(*args, **kw) finally: lock.release() return newFunction return wrapif __name__ == '__main__': from threading import Thread, Lock import time myLock = Lock() class MyThread(Thread): def __init__(self, n): Thread.__init__(self) self.n = n @synchronized(myLock) def run(self): """ Print out some stuff. The method sleeps for a second each iteration. If another thread were running, it would execute then. But since the only active threads are all synchronized on the same lock, no other thread will run. """ for i in range(5): print 'Thread %d: Start %d...' % (self.n, i), time.sleep(1) print '...stop [%d].' % self.n threads = [MyThread(i) for i in range(10)] for t in threads: t.start() for t in threads: t.join()
相关内容
- 初学python,,初学python[Pyt
- python实现多线程下载,python实现多线程,python实现多线程
- 金山快盘自动签到程序,金山签到程序,自动签到领取积
- python多线程Ping网段,python多线程ping,通过修改本站一个
- python用多个分隔符拆分字符串,python分隔符,如下代码,
- python计算指定两点经纬度之间的距离,python经纬度,下面
- python urllib quote或者quote_plus 抛出keyError的解决方案,u
- python2.7将doc文件转换成文本文件(windows),python2.7do
- python二分法查找,python二分法,def binary_s
- python列表操作extend和append的区别演示代码,pythonextend
评论关闭