简体   繁体   中英

Handling Child Error in Python Multiprocessing with Nested Function

I want to write a function in Python 3.6 with multiprocessing, where workers execute a function

def f(x):
   .
   .

in such a way that every time an error is raised in the child process, that child process should restart.

This is my code:

for worker in workers:
 def nested(worker):
  try:
   `proc=multiprocessing.Process(target=f,\
     args=(args[worker],))
    proc.start()
   except:
      nested(worker)
 nested(worker)

The problem is that this structure does not catch the error in the child process, so it does not work as intended. Unfortunately, the solutions in Python Multiprocessing: Handling Child Errors in Parent are very specific to the problem in that thread and cannot really be applied here.

Does anyone have an idea how to fix this?

With the help of @zwer, I got this to work:

def nested(args):
    try:
        f(args)
    except:
        nested(args)

for worker in workers:
    proc=multiprocessing.Process(target=nested,\
                    args=args[worker])
    proc.start()

Just beware that this structure does only work on Unix systems, for Windows, you need to put the function in a separate file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM