We can create non-forked processes with Python 3's multiprocessing using a created context:
ctx = multiprocessing.get_context('spawn')
p = ctx.Process(target=foo, args=(42,))
p.start()
But suppose I'm working with a subclass of Process. Is there a way to create a Process subclass instance using a method other than fork?
Inherit from ctx.Process:
ctx = multiprocessing.get_context('spawn')
class CustomProcess(ctx.Process):
# define methods
The accepted answer by @Daniel is perfectly fine (and may be more idiomatic), but note also that you can find the appropriately contextualized subclasses of Process deeper in the multiprocessing module (such as multiprocessing.context.SpawnProcess). You can also inherit from these to get the desired behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With