Extracting multiprocesses gracefully in python3

I would like to exit the program gracefully on Ctrl + C / SIGINT or on user input. If possible, the terminal should ask for something like: "Press Enter to finish."

Python 3.6 code to run

def worker(process):
    i = 0
    while True:
        print('Process %d count %d' % (process, i))
        i += 1

def main():
    available_num_cores = multiprocessing.cpu_count()
    use_num_cores = available_num_cores - 1 if available_num_cores > 1 else 1

    print('Using %d cores' % use_num_cores)

    pool = multiprocessing.Pool(use_num_cores)

    for i in range(0, use_num_cores):
        pool.apply_async(worker, args=(i,))
    pool.close()
    pool.join()

if __name__ == '__main__':
    main()

The accepted answer for this question is Cut Ctrl + C / SIGINT and gracefully output multiprocesses in python . It does not work, it crashes with an error:

Process SpawnPoolWorker-1:
Process 0 count 1572
Process SpawnPoolWorker-2:
Process 1 count 1472
Process SpawnPoolWorker-3:
Traceback (most recent call last):

Any help would be greatly appreciated. Thank!

+2
source share
1 answer

You must ensure that SIGINT is ignored by child processes.

CTRL + C.

def initializer():
    """Ignore SIGINT in child workers."""
    signal.signal(signal.SIGINT, signal.SIG_IGN)


def main():
    try:
        pool = multiprocessing.Pool(use_num_cores, initializer=initializer)

        for i in range(0, use_num_cores):
            pool.apply_async(worker, args=(i,))

        pool.close()

        input("Hit enter to terminate")
    except KeyboardInterrupt:
        print("CTRL+C")
    finally:
        pool.terminate()
        pool.join()        

        print("Bye have a great time!")
0

Source: https://habr.com/ru/post/1685468/


All Articles