-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_multprocessing.py
More file actions
52 lines (40 loc) · 1.03 KB
/
test_multprocessing.py
File metadata and controls
52 lines (40 loc) · 1.03 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
46
47
48
49
50
51
52
# -*- coding:utf-8 -*-
# 多进程
from multiprocessing import Process, Queue, Pool
from multiprocessing import current_process
import time
def start(i):
time.sleep(1)
print(i)
print(current_process().name)
print(current_process().pid)
print(current_process().is_alive())
return i
def write(q):
print("process to write: {}".format(current_process().pid))
for i in range(5):
print("put {} into q".format(i))
q.put(i)
def read(q):
print("process to read: {}".format(current_process().pid))
while True:
value = q.get()
print("get {} from queue".format(value))
def main():
print("--start--")
'''
q = Queue()
p1 = Process(target=write, args=(q,), name='p1')
p2 = Process(target=read, args=(q,), name='p2')
p1.start()
p2.start()
'''
# 进程池
pool = Pool(processes=4)
pool_output = pool.apply(start, args=(1,))
pool.close()
pool.join()
print(pool_output)
print("--stop--")
if __name__ == '__main__':
main()