-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-multi_thread_lock_demo_after.py
64 lines (46 loc) · 2.39 KB
/
04-multi_thread_lock_demo_after.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import time
from threading import Thread, Lock
from img_download_info import IMG_DOWNLOAD_INFO
class DownloadResizeImagesService(object):
def __init__(self, img_dl_time_dict):
self._img_dl_time_dict = img_dl_time_dict
self._total_downloaded_bytes = 0 # !!!!演示lock
self._total_downloaded_bytes_lock = Lock() # !!!!演示lock
def _download_image(self, img_id, dl_time):
print("[download_image]Start: download {}".format(img_id))
start = time.perf_counter()
time.sleep(dl_time)
with self._total_downloaded_bytes_lock: # 保证with里面的变量不会被多个子线程一起占用。一个问题: 共享变量什么时候用lock,什么时候可以不用?
self._total_downloaded_bytes += 1000 # 假设每个img都是1000 byte大小
end = time.perf_counter()
print("[download_image]End: downloaded {} in {} seconds".format(img_id, end - start))
def download_images(self):
print("[download_images]Start: -----------")
start = time.perf_counter()
dl_threads = []
for img_id, dl_time in self._img_dl_time_dict.items():
t = Thread(target=self._download_image, args=(img_id, dl_time))
t.start()
dl_threads.append(t)
for t in dl_threads:
t.join()
end = time.perf_counter()
print("[download_images]End: downloaded {} images in {} seconds".format(len(self._img_dl_time_dict), end - start))
def resize_images(self):
print("[resize_images]Start: -----------")
start = time.perf_counter()
for _ in range(len(self._img_dl_time_dict)):
# 假设每个img的resize只要0.1秒
time.sleep(0.1)
end = time.perf_counter()
print("[resize_images]End: resized {} images in {} seconds".format(len(self._img_dl_time_dict), end - start))
def download_resize_images(self):
print("[download_resize_images]Start: =============")
start = time.perf_counter()
self.download_images()
self.resize_images()
end = time.perf_counter()
print("[download_resize_images]End: download_resize_images in {} seconds".format(end - start))
service = DownloadResizeImagesService(IMG_DOWNLOAD_INFO)
service.download_resize_images()
print("private __total_downloaded_bytes: " + str(service._total_downloaded_bytes))