MAYA 多线程
''' Usage: def timerTest(): print 'Hello World!' #create and start a timer timer = Timer(30, timerTest, repeat=True) timer.start() #To stop the timer timer.stop() ''' import threading try: from maya.utils import executeInMainThreadWithResult except: executeInMainThreadWithResult = None class Timer(threading.Thread): def __init__(self, interval, function, args=[], kwargs={}, repeat=True): self.interval = interval self.function = function self.repeat = repeat self.args = args self.kwargs = kwargs self.event = threading.Event() threading.Thread.__init__(self) def run(self): def _mainLoop(): self.event.wait(self.interval) if not self.event.isSet(): if executeInMainThreadWithResult: executeInMainThreadWithResult(self.function, *self.args, **self.kwargs) else: self.function(*self.args, **self.kwargs) if self.repeat: while not self.event.isSet(): _mainLoop() else: _mainLoop() self.stop() def start(self): self.event.clear() threading.Thread.start(self) def stop(self): self.event.set() threading.Thread.__init__(self)
作者:jonn
出处:http://www.cnblogs.com/jonn/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-jonn。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步