CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day13/multithread2.py
Views: 729
1
"""
2
使用多线程的情况 - 模拟多个下载任务
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-20
7
"""
8
9
from random import randint
10
from threading import Thread
11
from time import time, sleep
12
13
14
def download_task(filename):
15
print('开始下载%s...' % filename)
16
time_to_download = randint(5, 10)
17
sleep(time_to_download)
18
print('%s下载完成! 耗费了%d秒' % (filename, time_to_download))
19
20
21
def main():
22
start = time()
23
thread1 = Thread(target=download_task, args=('Python从入门到住院.pdf',))
24
thread1.start()
25
thread2 = Thread(target=download_task, args=('Peking Hot.avi',))
26
thread2.start()
27
thread1.join()
28
thread2.join()
29
end = time()
30
print('总共耗费了%.3f秒' % (end - start))
31
32
33
if __name__ == '__main__':
34
main()
35
36