How to complete a childprocess's job when the parent is interrupted?

112 Views Asked by At

I run the following code, and I want the child process to continue to complete the job when the parent process is terminated.

I need the child process to complete each submitted job. The parent process doesn't matter.

import signal
from concurrent.futures import ProcessPoolExecutor
import time


def sub_task(bulks):
    def signal_handler(signal, frame):
        pass

    signal.signal(signal.SIGINT, signal_handler)

    for i in bulks:
        time.sleep(0.1)
    print("Success")
    return True


def main():
    # it is 40 cores
    pool = ProcessPoolExecutor(4)
    bulks = []
    try:
        for i in range(10000):
            time.sleep(0.05)
            bulks.append(i)

            if len(bulks) >= 100:
                print("Send")
                pool.submit(sub_task, bulks)
                bulks = []

        if bulks:
            pool.submit(sub_task, bulks)
    except:
        import traceback
        traceback.print_exc()
        pool.shutdown(wait=True)

    return True


main()

But got this exception:

Send
Send
Success
Send
^CProcess ForkProcess-4:
Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/usr/lib/python3.8/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python3.8/concurrent/futures/process.py", line 233, in _process_worker
    call_item = call_queue.get(block=True)
  File "/usr/lib/python3.8/multiprocessing/queues.py", line 97, in get
    res = self._recv_bytes()
  File "/usr/lib/python3.8/multiprocessing/connection.py", line 216, in recv_bytes
    buf = self._recv_bytes(maxlength)
  File "/usr/lib/python3.8/multiprocessing/connection.py", line 414, in _recv_bytes
    buf = self._recv(4)
  File "/usr/lib/python3.8/multiprocessing/connection.py", line 379, in _recv
    chunk = read(handle, remaining)
KeyboardInterrupt
Traceback (most recent call last):
  File "test1.py", line 37, in main
    time.sleep(0.05)
KeyboardInterrupt

Can anyone help me out?

Thanks in advance

0

There are 0 best solutions below