zoukankan      html  css  js  c++  java
  • python 线程 threading 模块 -1

    threading 

    import threading
    from time import sleep, ctime
    
    loops = [4, 2]
    
    
    class ThreadFunc(object):
        def __init__(self, func, args, name=''):
            self.name = name
            self.func = func
            self.args = args
    
        def __call__(self, *args, **kwargs):
            self.func(*self.args)
    
    
    def loop(nloop, nsec):
        print('start loop', nloop, 'at:', ctime())
        sleep(nsec)
        print('loop', nloop, 'done at:', ctime())
    
    
    def main():
        print('starting at:', ctime())
        threads = []
        nloops = range(len(loops))
        for i in nloops:
            # 创建线程
            t = threading.Thread(target=ThreadFunc(loop, (i, loops[i]), loop.__name__))
            threads.append(t)
        for i in nloops:
            threads[i].start()  # 启动线程
        for i in nloops:
            threads[i].join()  # 等待子线程完成时,主线程执行
        print('all DONE at:', ctime())
    
    
    if __name__ == '__main__':
        main()
    

      

  • 相关阅读:
    JS基础知识点2
    JS基础知识
    CSS-弹性盒子
    css取值
    css语法和规则
    第7-9章作业汇总
    第7-9章作业---第3题
    第四次作业——第一题
    第四次作业-第二题
    第四次作业---第四题
  • 原文地址:https://www.cnblogs.com/412013cl/p/10193825.html
Copyright © 2011-2022 走看看