python 多线程

来源:互联网 发布:无线传感器网络应用 编辑:程序博客网 时间:2024/06/06 08:37
#coding=utf-8import threadingfrom time import ctime,sleepdef Music(func):    for i in range(2):        print "I was listening to %s. %s \n" %(func,ctime())        sleep(1)def Run():    for i in range(2):        print "I was Run! %s \n" %(ctime())        sleep(3)threads = []t1 = threading.Thread(target=Music,args=('God is a girl',))threads.append(t1)t2 = threading.Thread(target=Run)threads.append(t2)if __name__ == '__main__':    for t in threads:        t.setDaemon(True)        t.start()    t.join()    print "all over %s" %ctime()

用python运行多线程,主要用到threading模块。t.join()保证子线程结束后运行主线程。

0 0
原创粉丝点击