Python-100-Days/Day01-15/code/Day13/multithread3.py

46 lines
1.1 KiB
Python
Raw Normal View History

2018-04-27 00:00:22 +08:00
"""
使用多线程的情况 - 模拟多个下载任务
Version: 0.1
Author: 骆昊
Date: 2018-03-20
"""
from random import randint
from time import time, sleep
import threading
class DownloadTask(threading.Thread):
2019-05-03 21:17:36 +08:00
def __init__(self, filename):
super().__init__()
self._filename = filename
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def run(self):
print('开始下载%s...' % self._filename)
time_to_download = randint(5, 10)
print('剩余时间%d秒.' % time_to_download)
sleep(time_to_download)
print('%s下载完成!' % self._filename)
2018-04-27 00:00:22 +08:00
def main():
2019-05-03 21:17:36 +08:00
start = time()
# 将多个下载任务放到多个线程中执行
# 通过自定义的线程类创建线程对象 线程启动后会回调执行run方法
thread1 = DownloadTask('Python从入门到住院.pdf')
thread1.start()
thread2 = DownloadTask('Peking Hot.avi')
thread2.start()
thread1.join()
thread2.join()
end = time()
print('总共耗费了%.3f' % (end - start))
2018-04-27 00:00:22 +08:00
if __name__ == '__main__':
2019-05-03 21:17:36 +08:00
main()
2018-04-27 00:00:22 +08:00
# 请注意通过threading.Thread创建的线程默认是非守护线程