久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

python多線程實現(xiàn)同時執(zhí)行兩個while循環(huán)的操作

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

如果想同時執(zhí)行兩個while True循環(huán),可以使用多線程threading來實現(xiàn)。

完整代碼

#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))#創(chuàng)建線程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多線程實現(xiàn)同時執(zhí)行兩個while循環(huán)的操作

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

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

>>> 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多線程實現(xiàn)同時執(zhí)行兩個while循環(huán)的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 和平县| 舒城县| 阜新| 平安县| 贵阳市| 阿拉善盟| 当雄县| 安顺市| 建水县| 临清市| 都昌县| 盘锦市| 凤山市| 宜黄县| 石嘴山市| 岳阳市| 中方县| 曲水县| 宁城县| 苗栗市| 多伦县| 奉节县| 景谷| 泗洪县| 瑞金市| 长沙市| 壤塘县| 墨江| 新闻| 武清区| 大同市| 额尔古纳市| 始兴县| 松溪县| 赣榆县| 亳州市| 远安县| 桐庐县| 深泽县| 恩施市| 灌阳县|