您的位置:首页 > 编程语言 > Python开发

python-多线程基础练习1

2018-01-15 13:11 302 查看
#coding=utf-8
import threading
from time import ctime,sleep

def music(func):
for i in range(2):
print("I was listening to music %s.%s" %(func,ctime()))
sleep(1)

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

"""
创建了threads数组,创建线程t1,使用threading.Thread()方法,在这个方法
中调用music方法target=music,args方法对music进行传参。 把创建好的线程t1装到threads数组中。
"""
threads = []
t1=threading.Thread(target=music,args=(u'爱情买卖',))
threads.append(t1)

t2=threading.Thread(target=move,args=(u'阿凡达',))
threads.append(t2)

if __name__=='__main__':
for t in threads:
t.setDaemon(True)
t.start()
t.join()
print("all over %s"%ctime())


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: