摘要: 多线程程序最令人头疼的便是各种锁,写起来麻烦也容易出错。 还好Python中有装饰器这种方便的神器存在,我们只需像单线程一样写程序 ,然后在外边包上同步锁装饰器就ok了。比如:import threadingdef sync(lock): def syncWithLock(fn): def newFn(*args,**kwargs): lock.acquire() try: return fn(*args,**kwargs) finally: lock.release() newFn.func_name = fn.func_name newFn.__doc__ = fn.__doc__ ret 阅读全文
posted @ 2011-05-31 16:21 simayixin 阅读(2632) 评论(0) 推荐(0) 编辑
摘要: 在编写服务进程的时候,往往需要定时落地一些数据,这就需要定时来执行一些操作,然后python中并没有合适的定时器(我没有找到),就自己diy吧。import threading,timeclass Timer(threading.Thread): def __init__(self,fn,args=(),sleep=0,lastDo=True): threading.Thread.__init__(self) self.fn = fn self.args = args self.sleep = sleep self.lastDo = lastDo self.setDaemon(True) se 阅读全文
posted @ 2011-05-31 15:54 simayixin 阅读(25291) 评论(2) 推荐(0) 编辑