[英]python multiprocessing is not giving output
I just learned about python multiprocessing and was trying to apply it in the following way:我刚刚了解了 python 多处理,并试图通过以下方式应用它:
Here this class A is going to run class B which inherits multiprocessing.Process在这里,这个 class A 将运行 class B 继承 multiprocessing.Process
import multiprocessing as mp
class A:
def __init__(self, num_workers=mp.cpu_count()):
self.num_workers = num_workers
self.x = 5
def process(self):
workers = []
for i in range(self.num_workers):
workers.append(B(self.x))
for worker in workers:
worker.start()
for worker in workers:
worker.join()
class B(mp.Process):
def __init__(self, val):
mp.Process.__init__(self)
self.val = val
def square(self):
print(self.val * self.val)
def run(self):
self.square()
Finally calling the classes to run the square function in this way:最后调用类以这种方式运行正方形 function:
a = A()
a.process()
But I am not getting any output.但我没有得到任何 output。
Note that this is a dummy micro version of another code which I am trying to convert multi-thread to multiprocessing due to some issue.请注意,这是另一个代码的虚拟微版本,由于某些问题,我正在尝试将多线程转换为多处理。
Call your classes like so像这样调用你的课程
if __name__ == '__main__':
a = A()
a.process()
Read Safe importing of main module for more info on why阅读主模块的安全导入以获取有关原因的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.