-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_threading.py
More file actions
35 lines (29 loc) · 812 Bytes
/
multi_threading.py
File metadata and controls
35 lines (29 loc) · 812 Bytes
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
from concurrent.futures import ThreadPoolExecutor, as_completed
import threading
import time
from datetime import datetime
'''
Suitable for parallel executing IO intensive tasks
'''
def worker(params):
output = []
for i in params:
print(datetime.now(), threading.get_ident())
output.append(i * 2)
time.sleep(1)
return output
pool = ThreadPoolExecutor(max_workers=10)
tasks = list(range(1, 300))
task_size = len(tasks)
batch_size = 30
futures = []
for i in range(0, task_size, batch_size):
print(i, min(i+batch_size, task_size))
batch = tasks[i:min(i+batch_size, task_size)]
futures.append(pool.submit(worker, batch))
pool.shutdown(wait=True)
result = []
for future in as_completed(futures):
items = future.result()
result.extend(items)
print(result)