-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyprocess.py
More file actions
executable file
·45 lines (34 loc) · 1.25 KB
/
pyprocess.py
File metadata and controls
executable file
·45 lines (34 loc) · 1.25 KB
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
#!/usr/bin/env python3
#
# Example on how to use ProcessPoolExecutor from the concurrent.futures library
#
import requests
import time
import json
from concurrent.futures import ProcessPoolExecutor
# Function to download data from internet with an artificial delay
def wget(url):
resp = requests.get(url)
time.sleep(2)
return resp.json()
# Start meaduring time
start_time = time.time()
# Start of Process Pooling with 3 executors
with ProcessPoolExecutor(max_workers=3) as executor:
# Assign a variable to the Future object generated by the executed function
comments_json = executor.submit(wget, ('https://jsonplaceholder.typicode.com/comments'))
photos_json = executor.submit(wget, ('https://jsonplaceholder.typicode.com/photos'))
albums_json = executor.submit(wget, ('https://jsonplaceholder.typicode.com/albums'))
# Extract the results from the Future object
comments_json = comments_json.result()
albums_json = albums_json.result()
photos_json = photos_json.result()
# Stop measuring time
stop_time = time.time()
# Print data type
print(type(comments_json))
print(type(albums_json))
print(type(photos_json))
print(json.dumps(comments_json, indent=2))
# Print elapsed time
print('Elapsed time in seconds: {}'.format(stop_time - start_time))