[Python] How to easily drop a child process started by multiprocess from another process

When I was writing a test for a program that runs as a daemon, I needed to kill it after a certain amount of time.

Requirements

 main process
   |
   |--- subprocess1
   |--- subprocess2
   |--- subprocess3

The subprocesses are started as follows.

# main.py
from multiprocessing import Process

ps = [
    Process(target=hogehoge1),
    Process(target=hogehoge2),
    Process(target=hogehoge3),
]
for i in ps:
    i.start()

Thing you want to do

Erase all subprocesses from another process in some way.

First SIGINT (ctrl + c)

When I'm running it in the terminal, I can erase everything with this one, so I'll try it.

# other.py
import subprocess
from time import sleep
from signal import SIGINT

proc = subprocess.Popen(cmd)
sleep(5) #wait a little
proc.send_signal(SIGINT)

result

Only the parent disappears and the subprocesses remain.

Catch SIGINT as a parent

This way it worked. The parent should have a signal (from the previous experiment), so let's remove the subprocess from the parent. Modify the parent file.

# main.py
from signal import SIGINT
import signal

def signalHandler(signal, handler) :
    for i in ps:
        i.terminate()
    exit(0)

if __name__ == "__main__":
    signal.signal(signal.SIGINT,  signalHandler)
    signal.signal(signal.SIGTERM, signalHandler)

Note: If you forget ʻexit (0)`, you will not be able to kill the parent process.

Summary

It's not good to multi-process in an atmosphere.

Recommended Posts

[Python] How to easily drop a child process started by multiprocess from another process
How to open a web browser from python
How to generate a Python object from JSON
How to save a table scraped by python to csv
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
[Python] How to make a list of character strings character by character
How to slice a block multiple array from a multiple array in Python
How to run a Python program from within a shell script
How to launch AWS Batch from a python client app
How to override a user-defined method generated by python swig
How to write a Python class
How to get started with Python
How to access wikipedia from python
Python to switch from another language
How to get a string from a command line argument in python
[Python] How to get & change rows / columns / values from a table.
Node.js: How to kill offspring of a process started with child_process.fork ()
Pass a list by reference from Python to C ++ with pybind11
How to remove duplicates from a Python list while preserving order.
How to create a clone from Github
How to easily convert format from Markdown
[Python] How to make a class iterable
Execute a script from Jupyter to process
[Python] How to convert a 2D list to a 1D list
How to update Google Sheets from Python
Send a message from Python to Slack
[Python] How to invert a character string
How to get a value from a parameter store in lambda (using python)
How to get a stacktrace in python
How to access RDS from Lambda (python)
How to easily operate IOT home appliances from Siri by API hacking
How to create a repository from media
How to run a Maya Python script
How to sort by specifying a column in the Python Numpy array.
How to drop Google Docs in one folder in a .txt file with python
[python] How to sort by the Nth Mth element of a multidimensional array
How to install a Python library that can be used by pharmaceutical companies
Send a message from Slack to a Python server
[python] How to display list elements side by side
How to read a CSV file with Python 2/3
How to create a Python virtual environment (venv)
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a function object from a string
Study from Python Hour7: How to use classes
How to create a JSON file in Python
[Python] How to read data from CIFAR-10 and CIFAR-100
How to add a Python module search path
How to handle Linux commands well from Python
How to extract coefficients from a fractional formula
How to erase the characters output by Python
[Python] How to draw a histogram in Matplotlib
[Python] How to sort instances by instance variables
Read line by line from a file with Python
A layman wants to get started with Python
How to publish GitHub Pages with Pelican, a static HTML generator made by Python
How to create a Python 3.6.0 environment by putting pyenv on Amazon Linux and Ubuntu
How to check in Python if one of the elements of a list is in another list
[Natural language processing / NLP] How to easily perform back translation by machine translation in Python