I just learned about python multiprocessing and was trying to apply it in the following way:
Here this class A is going to run class B which inherits 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:
a = A()
a.process()
But I am not getting any 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.