zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    oracle11g 新特性
    RMAN 报:ORA-19504 ORA-27038
    ORACLE-用户常用数据字典的查询使用方法
    oracle
    收缩 表空间
    oracle 配置 oem
    索引大小及占表的空间
    Oracle 11g Windows 迁移至 Linux
    Python:列表生成式
    Python:字符串处理函数
  • 原文地址:https://www.cnblogs.com/jonn/p/3887906.html
Copyright © 2011-2022 走看看