5290589 发表于 2018-8-12 10:02:34

Python之threading模块简单使用

# cat mtsleep3.py  
#!/usr/bin/env python
  

  
import threading
  
from time import sleep, ctime
  

  
loops =
  

  
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=loop,
  
                   args=(i, loops))
  
      threads.append(t)
  

  
    for i in nloops:
  
      threads.start()
  

  
    for i in nloops:
  
      threads.join()
  

  
    print 'all DONE at:', ctime()
  

  
if __name__ == '__main__':
  
    main()
页: [1]
查看完整版本: Python之threading模块简单使用