Python: can a child process pause / resume a parent process

I am trying to make a temperature monitoring module that continuously saves the current temperature in a file, and then uses matplotlib to draw a graph as soon as it is used for decoration. This functionality works for me, so I can use it as:

with TemperatureMonitoring(): # do stuff 

When __enter__ called, the process begins, which is just an endless loop that sleeps and writes to the file, and when __exit__ receives the call, the process ends and the file is displayed on the screen.

Now I want to make improvements, so I want the child process to control the parent; if the temperature is too high for too long, it will stop and wait for the computer to cool. This is my first time with the multiprocessing module, but it seems that if I pause the main process, the child also pauses. Therefore, if I reach a critical state, he will not be able to disconnect himself. Thus, the parent should be able to interrupt it when the code has completed execution, and the child should be able to pause / resume the parent process, if necessary. Is there an obvious way to do this?

+4
source share
2 answers

The cleanest way to do this is to use duprocess multiprocessing.Pipe . Then the parent can send the command to the child device to complete the work, and the child can send a notification to the parents about the temperature level.

+1
source

Using shared memory is the fastest IPC (interprocess communication).

You can use the posix_ipc package to create shared memory and semaphores. Shared memory will keep the temperature, and semaphores will allow you to revitalize the process and continue.

When it comes to completing the process, I propose doing this using a condition in the main loop. so that you can close all descriptors.

0
source

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


All Articles