Skip to content

Commit bbcbacc

Browse files
committed
Python Threading Tutorial
1 parent b66bd47 commit bbcbacc

19 files changed

+77
-0
lines changed

Python/Threading/download-images.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import requests
2+
import time
3+
import concurrent.futures
4+
5+
img_urls = [
6+
'https://images.unsplash.com/photo-1516117172878-fd2c41f4a759',
7+
'https://images.unsplash.com/photo-1532009324734-20a7a5813719',
8+
'https://images.unsplash.com/photo-1524429656589-6633a470097c',
9+
'https://images.unsplash.com/photo-1530224264768-7ff8c1789d79',
10+
'https://images.unsplash.com/photo-1564135624576-c5c88640f235',
11+
'https://images.unsplash.com/photo-1541698444083-023c97d3f4b6',
12+
'https://images.unsplash.com/photo-1522364723953-452d3431c267',
13+
'https://images.unsplash.com/photo-1513938709626-033611b8cc03',
14+
'https://images.unsplash.com/photo-1507143550189-fed454f93097',
15+
'https://images.unsplash.com/photo-1493976040374-85c8e12f0c0e',
16+
'https://images.unsplash.com/photo-1504198453319-5ce911bafcde',
17+
'https://images.unsplash.com/photo-1530122037265-a5f1f91d3b99',
18+
'https://images.unsplash.com/photo-1516972810927-80185027ca84',
19+
'https://images.unsplash.com/photo-1550439062-609e1531270e',
20+
'https://images.unsplash.com/photo-1549692520-acc6669e2f0c'
21+
]
22+
23+
t1 = time.perf_counter()
24+
25+
26+
def download_image(img_url):
27+
img_bytes = requests.get(img_url).content
28+
img_name = img_url.split('/')[3]
29+
img_name = f'{img_name}.jpg'
30+
with open(img_name, 'wb') as img_file:
31+
img_file.write(img_bytes)
32+
print(f'{img_name} was downloaded...')
33+
34+
35+
with concurrent.futures.ThreadPoolExecutor() as executor:
36+
executor.map(download_image, img_urls)
37+
38+
39+
t2 = time.perf_counter()
40+
41+
print(f'Finished in {t2-t1} seconds')
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

Python/Threading/threading-1.svg

Lines changed: 2 additions & 0 deletions
Loading

Python/Threading/threading-2.svg

Lines changed: 2 additions & 0 deletions
Loading

Python/Threading/threading-demo.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import concurrent.futures
2+
import time
3+
4+
start = time.perf_counter()
5+
6+
7+
def do_something(seconds):
8+
print(f'Sleeping {seconds} second(s)...')
9+
time.sleep(seconds)
10+
return f'Done Sleeping...{seconds}'
11+
12+
13+
with concurrent.futures.ThreadPoolExecutor() as executor:
14+
secs = [5, 4, 3, 2, 1]
15+
results = executor.map(do_something, secs)
16+
17+
# for result in results:
18+
# print(result)
19+
20+
# threads = []
21+
22+
# for _ in range(10):
23+
# t = threading.Thread(target=do_something, args=[1.5])
24+
# t.start()
25+
# threads.append(t)
26+
27+
# for thread in threads:
28+
# thread.join()
29+
30+
finish = time.perf_counter()
31+
32+
print(f'Finished in {round(finish-start, 2)} second(s)')

0 commit comments

Comments
 (0)