Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multiprocessing: Pool of custom Processes

I am subclassing the Process class, into a class I call EdgeRenderer. I want to use multiprocessing.Pool, except instead of regular Processes, I want them to be instances of my EdgeRenderer. Possible? How?

like image 782
Ram Rachum Avatar asked Apr 11 '09 21:04

Ram Rachum


2 Answers

From Jesse Noller:

It is not currently supported in the API, but would not be a bad addition. I'll look at adding it to python2.7/2.6.3 3.1 this week

like image 134
Ram Rachum Avatar answered Oct 24 '22 07:10

Ram Rachum


This seems to work:

import multiprocessing as mp

ctx = mp.get_context()  # get the default context

class MyProcess(ctx.Process):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        print("Hi, I'm custom a process")

ctx.Process = MyProcess  # override the context's Process

def worker(x):
    print(x**2)

p = ctx.Pool(4)
nums = range(10)
p.map(worker, nums)
like image 42
mskoh52 Avatar answered Oct 24 '22 07:10

mskoh52