Python OS module stalls after new processes starts

Question:

I am trying to write a Raspberry Pi Python-3 script that turns on an IR receiver using a call to os.system() with mode2 as the argument.

When this line of code executes, the mode2 process starts and runs properly (I can see it working in the open terminal) but the rest of the program stalls. There is additional code that needs to run after the os.system("mode2") call.

My question is: How do I start the mode2 process and still allow the rest of the python-3 code to continue executing?

Asked By: Carter ash

||

Answers:

Here is a minimal way to approach this:

A bash script my_process.sh sleeps for 10 seconds, then writes the string "Hello" into a world.txt file:

#!/usr/bin/env bash

sleep 10; echo 'Hello' > world.txt

A python script starts my_process.sh, and prints "Done from Python" when finished.

# File: sample_script.py
import subprocess

if __name__ == "__main__":
    subprocess.Popen(["bash", "my_process.sh"])
    print("Done from Python.")

Running sample_script.py will write "Done from Python" to the console, and the world.txt file will appear about ten seconds later.

Answered By: Alexander L. Hayes