charleslparker
charleslparker

Reputation: 2024

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?

Upvotes: 2

Views: 674

Answers (2)

charleslparker
charleslparker

Reputation: 2024

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.

Upvotes: 5

Daniel
Daniel

Reputation: 42758

Inherit from ctx.Process:

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

Upvotes: 6

Related Questions