在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ python 中的主線程 和子線程的關(guān)系

python 中的主線程 和子線程的關(guān)系

#coding=utf-8
import threading
from time import ctime,sleep
def music(func):
    for i in range(2):
        print "I was listening to %s. %s" %(func,ctime())
        sleep(1)

def move(func):
    for i in range(2):
        print "I was at the %s! %s" %(func,ctime())
        sleep(1)

threads = []
t1 = threading.Thread(target=music,args=(u'愛情買賣',))
threads.append(t1)
t2 = threading.Thread(target=move,args=(u'阿凡達(dá)',))
threads.append(t2)
if __name__ == '__main__':
    for t in threads:
        t.setDaemon(True)
        t.start()


    print "all over %s" %ctime()
    
    
    
    

這里的 print "all over %s" %ctime() 是主線程?為啥?
為啥python2和python3 執(zhí)行的結(jié)果不一樣呢?

python 2:
I was listening to 愛情買賣. Thu Dec 14 19:28:49 2017
all over Thu Dec 14 19:28:49 2017
python3:
I was listening to 愛情買賣. Thu Apr 17 12:51:45 2014 I was at the 阿凡達(dá)! Thu Apr 17 12:51:45 2014 all over Thu Apr 17 12:51:45 2014

回答
編輯回答
壞脾滊

以頂層腳本運行時,模塊名字會被設(shè)置為__main__,所以if name == '__main__':塊中的內(nèi)容自然是主線程。
你并沒有對IO輸出加鎖,所以print打出來的結(jié)果是什么樣子都有可能。和Python版本沒關(guān)系

2017年1月20日 12:41