@linux1s1s
2017-08-09T18:39:16.000000Z
字数 857
阅读 1312
Python
2017-08
这里简要记录
Python核心编程
读书笔记 整段代码 可以直接运行
# -*- coding:gb18030 -*-
# 线程同步 Threading
import threading
from time import sleep, ctime
loops = [4, 2]
def loop(nloop, during):
print 'Lopper ', nloop, 'start at: ', ctime()
sleep(during)
print 'Lopper ', nloop, 'stop at: ', ctime()
def main():
print 'Lopper start at: ', ctime()
nloops = range(len(loops))
threads = []
for i in nloops:
t = threading.Thread(target=loop, args=(i, loops[i]))
threads.append(t)
for i in nloops: # Start threads
threads[i].start()
for i in nloops: # Wait for all
threads[i].join()
print 'Lopper stop at: ', ctime()
if __name__ == '__main__':
main()
C:\Python27\python.exe H:/workspace/python-hw/hw-6.py
Lopper start at: Wed Aug 09 18:39:10 2017
Lopper 0 start at: Wed Aug 09 18:39:10 2017
Lopper 1 start at: Wed Aug 09 18:39:10 2017
Lopper 1 stop at: Wed Aug 09 18:39:12 2017
Lopper 0 stop at: Wed Aug 09 18:39:14 2017
Lopper stop at: Wed Aug 09 18:39:14 2017
Process finished with exit code 0