Running two python scripts in parallel

I am trying to run a web application on glitch. I have one Flask based python file that is already running (call that a.py). I want to have another application (b.py) running concurrently on the same project. Is this possible?

I have tried: ` “start”: “PYTHONUNBUFFERED=true python3 a.py && python3 b.py” in my glitch.json but it does not seem to work.

Any advise would be super helpful!
Thanks

Hello,

I’m not sure if this is going to work and I have not tested it.
You could create a start-point python file start.py that runs the other two files concurrently using threading and shell command.

Ressources

Sample code (NOT TESTED - may need some tweaking)

import os
import threading

def execute_script(name):
    os.system('python ' + name)

// Execute a.py on a thread (concurrently)
a = threading.Thread(target=execute_script, args=('a.py',))
a.start()

// Execute b.py on a thread (concurrently)
b = threading.Thread(target=execute_script, args=('b.py',))
b.start()

// Block main execution until a.py is terminated
a.join()
// Block main execution until b.py is terminated
b.join()

Hope this helps.

Also note that you can change PYTHONUNBUFFERED=true python3 a.py && python3 b.py to run the processes in the background: PYTHONUNBUFFERED=true python3 a.py & python3 b.py &. However, @Joprocorp’s solution using threading from python itself is the best way to go, especially as you get far more control over the scripts.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.