python多執行緒示例

lsq_008發表於2016-07-07
#coding=utf-8
import threading
from time import ctime,sleep


def music(func):
print "I was listening to %s. %s" % (func,ctime())
sleep(4)
print "I was finished %s. %s" % (func,ctime())

def move(func):
print "I was at the %s! %s"%(func,ctime())
sleep(6)
print "I was finished %s. %s" % (func,ctime())

threads = []
t1 = threading.Thread(target=music,args=(u'愛情買賣',))
threads.append(t1)
t2 = threading.Thread(target=move,args=(u'阿凡達',))
threads.append(t2)

if __name__=='__main__':
for t in threads:
t.setDaemon(True)
t.start()

for t in threads:
t.join()
print "all over %s "%ctime()

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10972173/viewspace-2121656/,如需轉載,請註明出處,否則將追究法律責任。

相關文章