-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultithreading.py
96 lines (70 loc) · 2.56 KB
/
multithreading.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#import threading
import time
import concurrent.futures
start = time.perf_counter()
def do_something(seconds):
print(f'Sleeping {seconds} second(s)..')
time.sleep(seconds)
return f'Done sleeping...{seconds}'
#t1 = threading.Thread(target=do_something)
#t2 = threading.Thread(target=do_something)
#
#t1.start()
#t2.start()
#
#t1.join()
#t2.join()
#threads = []
#
#for _ in range(1000):
# t = threading.Thread(target=do_something, args=[1.5])
# t.start()
# threads.append(t)
#
#for thread in threads:
# thread.join()
#
with concurrent.futures.ThreadPoolExecutor() as executor:
secs = [5,4,3,2,1, 10]
#results = [executor.submit(do_something, sec) for sec in secs]
results = executor.map(do_something, secs)
for result in results:
print(result)
#for f in concurrent.futures.as_completed(results):
# print(f.result())
#f1 = executor.submit(do_something, 1)
#f2 = executor.submit(do_something, 2)
finish = time.perf_counter()
print(f'Finished in {round(finish-start, 2)} second(s)')
import requests
import time
import concurrent.futures
img_urls = [
'https://images.unsplash.com/photo-1516117172878-fd2c41f4a759',
'https://images.unsplash.com/photo-1532009324734-20a7a5813719',
'https://images.unsplash.com/photo-1524429656589-6633a470097c',
'https://images.unsplash.com/photo-1530224264768-7ff8c1789d79',
'https://images.unsplash.com/photo-1564135624576-c5c88640f235',
'https://images.unsplash.com/photo-1541698444083-023c97d3f4b6',
'https://images.unsplash.com/photo-1522364723953-452d3431c267',
'https://images.unsplash.com/photo-1513938709626-033611b8cc03',
'https://images.unsplash.com/photo-1507143550189-fed454f93097',
'https://images.unsplash.com/photo-1493976040374-85c8e12f0c0e',
'https://images.unsplash.com/photo-1504198453319-5ce911bafcde',
'https://images.unsplash.com/photo-1530122037265-a5f1f91d3b99',
'https://images.unsplash.com/photo-1516972810927-80185027ca84',
'https://images.unsplash.com/photo-1550439062-609e1531270e',
'https://images.unsplash.com/photo-1549692520-acc6669e2f0c'
]
t1 = time.perf_counter()
def download_image(img_url):
img_bytes = requests.get(img_url).content
img_name = img_url.split('/')[3]
img_name = f'{img_name}.jpg'
with open(img_name, 'wb') as img_file:
img_file.write(img_bytes)
print(f'{img_name} was downloaded...')
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(download_image, img_urls)
t2 = time.perf_counter()
print(f'Finished in {t2-t1} seconds')