[关闭]
@linux1s1s 2017-08-09T18:39:16.000000Z 字数 857 阅读 1304

Python 核心编程笔记五

Python 2017-08


这里简要记录 Python核心编程 读书笔记 整段代码 可以直接运行

CODE-多线程同步-Threading

  1. # -*- coding:gb18030 -*-
  2. # 线程同步 Threading
  3. import threading
  4. from time import sleep, ctime
  5. loops = [4, 2]
  6. def loop(nloop, during):
  7. print 'Lopper ', nloop, 'start at: ', ctime()
  8. sleep(during)
  9. print 'Lopper ', nloop, 'stop at: ', ctime()
  10. def main():
  11. print 'Lopper start at: ', ctime()
  12. nloops = range(len(loops))
  13. threads = []
  14. for i in nloops:
  15. t = threading.Thread(target=loop, args=(i, loops[i]))
  16. threads.append(t)
  17. for i in nloops: # Start threads
  18. threads[i].start()
  19. for i in nloops: # Wait for all
  20. threads[i].join()
  21. print 'Lopper stop at: ', ctime()
  22. if __name__ == '__main__':
  23. main()

RESULT

  1. C:\Python27\python.exe H:/workspace/python-hw/hw-6.py
  2. Lopper start at: Wed Aug 09 18:39:10 2017
  3. Lopper 0 start at: Wed Aug 09 18:39:10 2017
  4. Lopper 1 start at: Wed Aug 09 18:39:10 2017
  5. Lopper 1 stop at: Wed Aug 09 18:39:12 2017
  6. Lopper 0 stop at: Wed Aug 09 18:39:14 2017
  7. Lopper stop at: Wed Aug 09 18:39:14 2017
  8. Process finished with exit code 0
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注