若是想同時執行兩個while True循環,可使用多線程threading來實現。html
完整代碼 多線程
#coding=gbk from time import sleep, ctime import threading def muisc(func): while True: print 'Start playing: %s! %s' %(func,ctime()) sleep(2) def move(func): while True: print 'Start playing: %s! %s' %(func,ctime()) sleep(5) def player(name): r = name.split('.')[1] if r == 'mp3': muisc(name) else: if r == 'mp4': move(name) else: print 'error: The format is not recognized!' list = ['愛情買賣.mp3','阿凡達.mp4'] threads = [] files = range(len(list)) #建立線程 for i in files: t = threading.Thread(target=player,args=(list[i],)) threads.append(t) if __name__ == '__main__': #啓動線程 for i in files: threads[i].start() for i in files: threads[i].join() #主線程 print 'end:%s' %ctime()
效果:app