Run multiple .py files at once.

I have three .py files that I want to run simultaneously in a Python script file. I initially called subprocess.call() three times (once for each .py file), but remembered that it was blocked until the command was completed. I tried subprocess.Popen(['screen', 'python_file']) , since I believe that it does not block, but when I checked processes with screen -ls , only one process was running. How to make all three programs work simultaneously with a Python script? Should I use the multiprocessing or multithreading library?

Edit: other processes should not end, as they work in an infinite loop. Here is exactly what I had in the Python script file. I use screen because every .py file has a stdout registered on the terminal and I want to be able to see what is logged for each.


subprocess.Popen(['screen', './submitter.py'])
subprocess.Popen(['screen', './worker.py'])
subprocess.Popen(['screen', './tester.py'])

+4
source share
1 answer

If you want to use multiprocessing , you can try the following:

 import multiprocessing def worker(file): # your subprocess code if __name__ == '__main__': files = ["path/to/file1.py","path/to/file2.py","path/to/file3.py"] for i in files: p = multiprocessing.Process(target=worker, args=(i,)) p.start() 
+7
source

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


All Articles