Control a python process form another python file
<p>first of all a short overview over my current goal:</p>

<p>I want to use a scheduler to execute a simple python program every second. This program reads some data and enter the results inside a database. Because the scheduled task will operate over several days on a raspberry pie the process should start in the background. Therefore I want to create a python file which can start, stop and get the current status from the background job. Furthermore it should be possible to exit and reenter the control file without stopping the background job.</p>

<p>Currently I tried apscheduler to execute the python file every second. The actual problem is, that I can't access the current python file, to control the status, from another external file. Overall I found no real solution how I can control a subprocess form an external file and after find the same subprocess again after restarting the controlling python file.</p>

<p>EDIT:</p>

<p>So overall as far I got it now I'm able to find the current process with his pid. With that im able to send send a terminatesignal to the current process. Inside my scheduled file I'm able to <a href="https://nattster.wordpress.com/2013/06/05/catch-kill-signal-in-python/" rel="nofollow">catch these signals</a> and shut down the program on a normal way.</p>

<p>To control (start, restart, stop, schedule) the background process use <a href="https://docs.python.org/3/library/subprocess.html" rel="nofollow">subprocess</a>. Here is <a href="http://stackoverflow.com/questions/1191374/using-module-subprocess-with-timeout">example</a> of subrocess' popen with timeout.</p>

<p>To pass some data between the <code>scheduler</code> and the <code>background job</code> use one of <a href="https://docs.python.org/3/library/ipc.html" rel="nofollow">IPC</a> mechanisms, for example sockets.</p>

python python-3.x subprocess scheduled-tasks