我正在尝试学习有关使用Python进行多处理的更多信息。我正在使用multiprocessing
模块,但无法理解它是如何工作的。
这些是为实现多处理而编写的函数
import multiprocessing as mp
import os
import random
zones = []
for i in range(0,10):
x = random.randint(1,10)
zones.append(x)
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]
def info(title):
print(title)
print( 'module name:', __name__)
if hasattr(os, 'getppid'): # only available on Unix
print('parent process:', os.getppid())
print( 'process id:', os.getpid())
def worker(x):
for i in range(0,5):
print( x * i)
def main():
man = mp.Manager()
split = chunks(zones, len(zones)//4)
for z in split:
info('==========')
p = mp.Process(target=worker,args=(z,))
print(p)
p.start()
if __name__ == '__main__':
mp.set_start_method('fork')
main()
我在shell上得到了这个输出
==========
module name: __main__
parent process: 5226
process id: 7774
<Process(Process-2, initial)>
==========
module name: __main__
parent process: 5226
process id: 7774
<Process(Process-3, initial)>
[]
[1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1]
==========
module name: __main__
parent process: 5226
process id: 7774
<Process(Process-4, initial)>
[]
[5, 2]
[5, 2, 5, 2]
[5, 2, 5, 2, 5, 2]
[5, 2, 5, 2, 5, 2, 5, 2]
==========
module name: __main__
parent process: 5226
process id: 7774
<Process(Process-5, initial)>
==========
module name: __main__
parent process: 5226
process id: 7774
[]
<Process(Process-6, initial)>
[5, 3]
[5, 3, 5, 3]
[5, 3, 5, 3, 5, 3]
[5, 3, 5, 3, 5, 3, 5, 3]
[]
[9, 10]
[9, 10, 9, 10]
[9, 10, 9, 10, 9, 10]
[9, 10, 9, 10, 9, 10, 9, 10]
[]
[1, 7]
[1, 7, 1, 7]
[1, 7, 1, 7, 1, 7]
[1, 7, 1, 7, 1, 7, 1, 7]
为什么过程if总是相同并且没有变化?因为我将处理拆分为多个部分,所以我希望代码在不同的进程ID上运行。
答案 0 :(得分:2)
您的工作人员中没有一个打电话给socket.on("draw", this.onDraw.bind(this));
。试试这个:
info
输出:
import multiprocessing as mp
import os
def info():
print("==")
print('module name:', __name__)
print('parent process:', os.getppid())
print('process id:', os.getpid())
def worker(arg):
info()
print("worker arg: {}".format(arg, ))
man = mp.Manager()
for z in [1,2,3,4]:
p = mp.Process(target=worker,args=(z,))
p.start()
答案 1 :(得分:0)
您是从主循环而不是子进程的循环中调用函数info
。
根据以下代码修改代码:
from multiprocessing import Process
import os
def info(title):
print(title)
print('module name:', __name__)
print('parent process:', os.getppid())
print('process id:', os.getpid())
## Function chunks used to divide the input files into slices
zones=[1,2,3,4]
for z in zones:
p = Process(target=info,args=(z,))
p.start()
p.join()
输出:
python multiprocess.py
1
('module name:', '__main__')
('parent process:', 47974)
('process id:', 47975)
2
('module name:', '__main__')
('parent process:', 47974)
('process id:', 47976)
3
('module name:', '__main__')
('parent process:', 47974)
('process id:', 47977)
4
('module name:', '__main__')
('parent process:', 47974)
('process id:', 47978)