-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_processing.py
More file actions
37 lines (31 loc) · 838 Bytes
/
multi_processing.py
File metadata and controls
37 lines (31 loc) · 838 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
36
37
from multiprocessing import Pool
import time
from datetime import datetime
import os
'''
Suitable for parallel executing computationally intensive tasks
'''
def worker(params):
output = []
for i in params:
print(datetime.now(), os.getpid())
output.append(i * 2)
time.sleep(1)
return output
if __name__ == '__main__':
pool = Pool(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.apply_async(worker, args=(batch,)))
pool.close()
pool.join()
result = []
for future in futures:
items = future.get()
result.extend(items)
print(result)