I have some code like the following:
import multiprocessing as mp
connection: module.Connection
def client_id():
for i in range(mp.cpu_count*2):
yield i
def initproc(host: str, port: int, client_id: int):
global connection
connection.connect(host, port, client_id)
def main():
host = "something"
port = 12345
mp.get_context("spawn").Pool(processes=mp.cpu_count()*2,
initializer=initproc,
initargs=(host, port, client_id())) as p:
res = p.starmap(processing_function, arg_list)
for the purposes of the question processing_function and arg_list are not relevant.
The issue is that I get an error with this:
ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle 'generator' object
Is there any way to create an initialize a process in the pool in such a way that on of the arguments to initialize it would be the next number in a sequence?
P.S. In the code as written it may be possible to initialize all connection objects outside of the initializer function but in my particular instance it is not. I need to pass arguments for connection into the initializer.