Python3: 将 class 作为单独的进程调用
Python3: Calling a class as a separate process
我正在使用 multiprocessing 中的 Process 库,我试图从另一个文件调用 class 作为一个单独的进程,但是,我收到此错误:
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文件:
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__()
正如@Jeronimo 在评论中所回答的那样 - 更改
p = Process(target=(call.Call(),)
到
p = Process(target=(call.Call))
是给出正确输出的解决方案:
2965 __main__
2966 CALL
internal func call
Leaving Call
Leaving main
为被调用的 class 使用单独的进程。
我正在使用 multiprocessing 中的 Process 库,我试图从另一个文件调用 class 作为一个单独的进程,但是,我收到此错误:
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文件:
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__()
正如@Jeronimo 在评论中所回答的那样 - 更改
p = Process(target=(call.Call(),)
到
p = Process(target=(call.Call))
是给出正确输出的解决方案:
2965 __main__
2966 CALL
internal func call
Leaving Call
Leaving main
为被调用的 class 使用单独的进程。