没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
2页
如果想同时执行两个while True循环,可以使用多线程threading来实现。 完整代码 #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 p
资源详情
资源评论
资源推荐
python多线程实现同时执行两个多线程实现同时执行两个while循环的操作循环的操作
如果想同时执行两个while True循环,可以使用多线程threading来实现。
完整代码完整代码
#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()
效果:
补充知识:补充知识: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)
# 输出
weixin_38716563
- 粉丝: 5
- 资源: 871
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0