国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python多線程實現同時執行兩個while循環的操作

瀏覽:111日期:2022-07-26 16:43:19

如果想同時執行兩個while True循環,可以使用多線程threading來實現。

完整代碼

#coding=gbkfrom time import sleep, ctime import threadingdef 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()

效果:

python多線程實現同時執行兩個while循環的操作

補充知識:python 如何在一個for循環中遍歷兩個列表

利用python自帶的zip函數可同時對兩個列表進行遍歷,代碼如下:

>>> list1 = [’a’, ’b’, ’c’, ’d’]>>> list2 = [’apple’, ’boy’, ’cat’, ’dog’]>>> for x, y in zip(list1, list2): print(x, ’is’, y)# 輸出a is appleb is boyc is catd is dog

以上這篇python多線程實現同時執行兩個while循環的操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 忻州市| 遂溪县| 南岸区| 天长市| 和林格尔县| 宜丰县| 安陆市| 渝北区| 藁城市| 曲麻莱县| 钟祥市| 绥化市| 吉林省| 曲沃县| 大同县| 和静县| 西充县| 苗栗市| 九江县| 洞口县| 潮安县| 定西市| 红桥区| 仪征市| 宣威市| 冷水江市| 太和县| 青河县| 沽源县| 平安县| 天柱县| 木兰县| 青河县| 平潭县| 吉木萨尔县| 彭阳县| 东台市| 内乡县| 越西县| 万源市| 千阳县|