-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtry.py
56 lines (41 loc) · 1.26 KB
/
try.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
from multiprocessing.pool import Pool
from time import sleep
def worker2(id):
print(f"worker2-id{id}")
def worker1(id):
print(f"worker1-id{id}")
for id in range(5):
pool.apply_async(worker2, args=(id,))
# def task(message):
# # report a message
# print(f"Task executing: {message}", flush=True)
# sleep(1)
# print(f"Task done: {message}", flush=True)
print("concurrent:")
pool = Pool()
for id in range(5):
pool.apply_async(worker1, args=(id,))
# pool.apply_async(task, args=("Hello world",))
pool.close()
pool.join()
# # SuperFastPython.com
# # example of issuing a task with apply_async() to the process pool with arguments
# from multiprocessing.pool import Pool
# # task executed in a worker process
# def task(message):
# # report a message
# print(f"Task executing: {message}", flush=True)
# # block for a moment
# sleep(1)
# # report a message
# print(f"Task done: {message}", flush=True)
# # protect the entry point
# if __name__ == "__main__":
# # create and configure the process pool
# pool = Pool()
# # issue tasks to the process pool
# pool.apply_async(task, args=("Hello world",))
# # close the process pool
# pool.close()
# # wait for all tasks to finish
# pool.join()