简体   繁体   English

在python中的并行进程中运行类方法

[英]Run a class method in parallel processes in python

I am trying and failing to run a huge loop in parallel. 我正在尝试并且未能并行运行一个巨大的循环。 The loop is exactly one method of a specific class, and inside the loop I call its another method. 循环恰好是特定类的一种方法,在循环内部我称之为另一种方法。 It does work, but for some reason there is only one process in the list and the output (see code) is always 'Worker 0'. 它确实可以工作,但是由于某种原因,列表中只有一个进程,并且输出(请参见代码)始终为“ Worker 0”。 Either the processes are not created or they are not running in parallel. 要么未创建进程,要么进程未并行运行。 The structure is the following: 结构如下:

main.py main.py

from my_class.py import MyClass

def main():
    class_object = MyClass()
    class_object.method()

if __name__ == '__main__':
    main()

my_class.py my_class.py

from multiprocessing import Process

MyClass(object):
    def __init__(self):
        # do something

    def _method(self, worker_num, n_workers, amount, job, data):
        for i, val in enumerate(job):
            print('Worker %d' % worker_num)
            self.another_method(val, data)

    def another_method(self):
        # do something to the data

    def method(self):
        # definitions of data and job_size go here

        n_workers = 16
        chunk = job_size // n_workers
        resid = job_size - chunk * n_workers

        workers = []
        for worker_num in range(n_workers):
            st = worker_num * chunk
            amount = chunk if worker_num != n_workers - 1 else chunk + resid
            worker = Process(target=self._method, args=[worker_num, n_workers, amount, job[st:st+amount], data])
            worker.start()
            workers.append(worker)

        for worker in workers:
            worker.join()

        return data

I have read some things about child processes requiring main module to be importable, but I have no idea how to do it in my case. 我已经阅读了一些有关需要主模块可导入的子进程的信息,但是我不知道该如何做。

Question : ... but still only one core is in use. 问题 :...但是仍然只使用一个内核。 So the question is, can I use multiple cores with Process objects 所以问题是,我可以将多个核与Process对象一起使用

This does not depend on the Python interpreter which Process is using which CPU. 这并不取决于Python解释器哪个Process使用哪个CPU。
Relevant: on-what-cpu-cores-are-my-python-processes-running 相关: 在什么CPU核心是我的Python进程运行

Extend your def _method(... with the following, to see what actually happens: 用以下内容扩展def _method(... ,以查看实际发生的情况:

Note : getpidcore(pid) is Distribution dependend, could FAIL ! 注意getpidcore(pid)分发的依赖项,可能会失败

def getpidcore(pid):
    with open('/proc/{}/stat'.format(pid), 'rb') as fh:
        core = int(fh.read().split()[-14])
        return core

class MyClass(object): 
    ...
    def _method(self, worker_num, n_workers, amount, job, data):
        for i, val in enumerate(job):
            core = getpidcore(os.getpid())
            print('core:{} pid:{} Worker({})'.format(core, os.getpid(), (worker_num, n_workers, amount, job)))

Output : 输出

 core:1 pid:7623 Worker((0, 16, 1, [1])) core:1 pid:7625 Worker((2, 16, 1, [3])) core:0 pid:7624 Worker((1, 16, 1, [2])) core:1 pid:7626 Worker((3, 16, 1, [4])) core:1 pid:7628 Worker((5, 16, 1, [6])) core:0 pid:7627 Worker((4, 16, 1, [5])) 

Tested with Python: 3.4.2 on Linux 经过Python测试:Linux上为3.4.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM