我正在尝试学习更多关于使用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]为什么这个过程总是相同的,而且不会改变?因为我将进程分成几个部分,所以我希望代码在不同的进程id上运行。
发布于 2019-02-18 19:30:18
你的员工都不会给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()输出:
==
module name: __main__
parent process: 2253
process id: 2256
worker arg: 1
==
module name: __main__
parent process: 2253
process id: 2258
worker arg: 3
==
module name: __main__
parent process: 2253
process id: 2257
worker arg: 2
==
module name: __main__
parent process: 2253
process id: 2259
worker arg: 4发布于 2019-02-18 19:37:04
您将从主进程而不是子进程的循环中调用函数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)https://stackoverflow.com/questions/54746043
复制相似问题