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

  • 相关阅读:
    pandas去重方法
    原生表单组件
    html表单
    html表格基本标签
    文档和网站架构
    文本格式
    【Leetcode链表】奇偶链表(328)
    【Leetcode链表】移除链表元素(203)
    【Leetcode链表】旋转链表(61)
    【Leetcode链表】反转链表 II(92)
  • 原文地址:https://www.cnblogs.com/jonn/p/3887906.html
Copyright © 2011-2022 走看看