[Python] How to handle initializer error in multiprocessing.Pool?

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

  1. Stack

    Stack Membro Participativo

    When initializer throw Error like below, script won't stop.
    I would like to abort before starting main process(do not run 'do_something').

    from multiprocessing import Pool
    import contextlib

    def initializer():
    raise Exception("init failed")

    def do_something(args):
    # main process
    pass

    pool = Pool(1, initializer=initializer)
    with contextlib.closing(pool):
    try:
    pool.map_async(do_something, [1]).get(100)
    except:
    pool.terminate()


    The never stopping stacktrace on console is below

    ...
    Exception: init failed
    Process ForkPoolWorker-18:
    Traceback (most recent call last):
    File "/home/hoge/anaconda3/lib/python3.6/multiprocessing/process.py", line 249, in _bootstrap
    self.run()
    File "/home/hoge/anaconda3/lib/python3.6/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
    File "/home/hoge/anaconda3/lib/python3.6/multiprocessing/pool.py", line 103, in worker
    initializer(*initargs)
    File "hoge.py", line 5, in initializer
    raise Exception("init failed")
    Exception: init failed
    ...


    My workaround is suppressing initializer error and return at the beginning of the main process by using global flag like below.
    But I would like to learn better one.

    def initializer():
    try:
    raise Exception("init failed")
    except:
    global failed
    failed = True

    def do_something(args):
    global failed
    if failed:
    # skip when initializer failed
    return
    # main process

    Continue reading...

Compartilhe esta Página