How to initialize each multithreading Pool worker with an individual value?

Valery Khamenya khamenya at
Tue Nov 30 22:35:32 CET 2010


multithreading.pool Pool has a promissing initializer argument in its
However it doesn't look possible to use it to initialize each Pool's
worker with some individual value (I'd wish to be wrong here)

So, how to initialize each multithreading Pool worker with the
individual values?

The typical use case might be a connection pool, say, of 3 workers,
where each of 3 workers has its own TCP/IP port.

from multiprocessing.pool import Pool

def port_initializer(_port):
    global port
    port = _port

def use_connection(some_packet):
    global _port
    print "sending data over port # %s" % port

if __name__ == "__main__":
    ports=((4001,4002, 4003), )
    p = Pool(3, port_initializer, ports) # oops... :-)
    some_data_to_send = range(20), some_data_to_send)

best regards
Valery A.Khamenya

More information about the Python-list mailing list