首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多进程信息的输出

多进程信息的输出
EN

Stack Overflow用户
提问于 2019-02-18 19:16:09
回答 2查看 39关注 0票数 1

我正在尝试学习更多关于使用Python进行多处理的知识。我正在使用multiprocessing模块,但我无法理解它是如何工作的。这些是为实现多进程而编写的函数

代码语言:javascript
复制
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上得到了这个输出

代码语言:javascript
复制
    ==========
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上运行。

EN

回答 2

Stack Overflow用户

发布于 2019-02-18 19:30:18

你的员工都不会给info打电话。试试这个:

代码语言:javascript
复制
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()

输出:

代码语言:javascript
复制
==
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
票数 2
EN

Stack Overflow用户

发布于 2019-02-18 19:37:04

您将从主进程而不是子进程的循环中调用函数info

根据以下内容调整代码:

代码语言:javascript
复制
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()

输出:

代码语言:javascript
复制
 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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54746043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档