Search code examples
pythonpython-3.xpython-multiprocessing

Python 3: Spawning Process Subclasses


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?


Solution

  • Inherit from ctx.Process:

    ctx = multiprocessing.get_context('spawn')
    class CustomProcess(ctx.Process):
        # define methods