[Python] Where should I allocate the pool in python multiprocessing?

Discussão em 'Python' iniciado por Stack, Setembro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I want to distribute a python app over several kernels and from the documentation I understand that a pool is the way to do this. My problem can be reproduced by the following code:

    #!/usr/bin/python3
    import multiprocessing as mp
    import multiprocessing.managers
    import time

    def poolswimmer():
    for i in range(5):
    print(i)
    time.sleep(1)

    def poolprocess(_pool):
    res = _pool.apply_async(poolswimmer)
    res.wait()

    if __name__ == "__main__":
    mp.freeze_support()
    mpc = mp.get_context('spawn')
    manager = mpc.Manager()
    pool = manager.Pool()
    p = mp.Process(target=poolprocess, args=(pool,))
    p.start()
    p.join()
    pool.close()


    is the I/O intensive part and is the CPU intensive part of a job which is executed in loops for different peripherals.
    I get the following error:

    Process Process-2:
    Traceback (most recent call last):
    File "/usr/lib/python3.12/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
    File "/usr/lib/python3.12/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
    File "/home/dad/sandbox/ImageProcessing/poolrider.py", line 12, in poolprocess
    res = _pool.apply_async(poolswimmer)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "<string>", line 2, in apply_async
    File "/usr/lib/python3.12/multiprocessing/managers.py", line 827, in _callmethod
    proxytype = self._manager._registry[token.typeid][-1]
    ^^^^^^^^^^^^^^^^^^^^^^^
    AttributeError: 'NoneType' object has no attribute '_registry'


    The object inspector tells me, that my pool variable is PoolProxy object, so it should be ok to call it from the process.
    Where am I wrong ?
    Is there a better way to achieve what I'm trying ?

    Continue reading...

Compartilhe esta Página