首页 > 解决方案 > Python3:调用一个类作为一个单独的进程

问题描述

我正在玩多处理中的 Process 库,我试图从另一个文件中调用一个类作为一个单独的进程,但是,我收到了这个错误:

Traceback (most recent call last):
File "/usr/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python3.6/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
TypeError: 'tuple' object is not callable

它仍然存在于与我的标准输出相同的过程中:

20472 __main__
20472 CALL
internal func call
Leaving Call
Process Process-1:
#This is where the error prints out
Leaving main

其中 20472 是 pid。

主文件:

import CALL
from multiprocessing import Process
import os

if __name__ == '__main__':
    print(os.getpid(),__name__)
    p = Process(target=(CALL.Call(),))
    p.start()
    p.join()
    print("Leaving main")

import os

调用类文件:

class Call():

    def __init__(self):
        print(os.getpid(), __name__)
        self.internal()

    def __exit__(self):
        print("Leaving Call")

    def internal(self):
        print("internal func call")
        self.__exit__()

标签: python-3.xclassprocessmultiprocessingpython-multiprocessing

解决方案


正如@Jeronimo 在评论中所回答的那样——改变

p = Process(target=(call.Call(),)

p = Process(target=(call.Call))

是给出正确输出的解决方案:

2965 __main__
2966 CALL
internal func call
Leaving Call
Leaving main

对于被调用的类有一个单独的进程。


推荐阅读