-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreading07.py
49 lines (35 loc) · 908 Bytes
/
Threading07.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# coding=utf-8
from time import sleep, ctime
import threading
def music(func):
for i in range(2):
print('Start playing: %s! %s' % (func, ctime()))
sleep(2)
def movie(func):
for i in range(2):
print('Start playing: %s! %s' % (func, ctime()))
sleep(5)
def player(name):
r = name.split('.')[1]
if r == 'mp3':
music(name)
else:
if r == 'mp4':
movie(name)
else:
print('error: The format is not recognized!')
lists = ['爱情买卖.mp3', '阿凡达.mp4', '金刚.mp4']
threads = []
files = range(len(lists))
# 创建线程
for i in files:
t = threading.Thread(target=player, args=(lists[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())