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

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


Hi,

multithreading.pool Pool has a promissing initializer argument in its
constructor.
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)
    p.map(use_connection, some_data_to_send)


best regards
--
Valery A.Khamenya



More information about the Python-list mailing list