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]