Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running multiple scripts from a single Script in python

I have two scripts Server.py and ServerGUI.py. I want them to run independently and in parallel. Say I make another script main.py. How can I run Server.py and ServerGUI.py from main.py?

Can you suggest me the code for main.py?

like image 981
Subhrasish Pal Avatar asked Apr 14 '26 06:04

Subhrasish Pal


1 Answers

To run 2 or more scripts from within a python script, you can use the subprocess package with nohup. This will run each script in the background allowing you to run them in parallel from the same source script. Also, as an option, this example will save the standard output from each script in a different file

import os
from subprocess import call
from subprocess import Popen


# subprocess.call(['python', 'exampleScripts.py', somescript_arg1, somescript_val1,...]).

Popen(['nohup', 'python', 'exampleScripts.py'],
                 stdout=open('null1', 'w'),
                 stderr=open('logfile.log', 'a'),
                 start_new_session=True )
                 
Popen(['nohup', 'python', 'exampleScripts.py'],
                 stdout=open('null2', 'w'),
                 stderr=open('logfile.log', 'a'),
                 start_new_session=True )

Popen(['nohup', 'python', 'exampleScripts.py'],
                 stdout=open('null3', 'w'),
                 stderr=open('logfile.log', 'a'),
                 start_new_session=True )      

Output: the start and end times in each script overlap showing that the 2nd one started before the first one ended

(ds_tensorflow) C:\DataScience\SampleNotebooks\Threading>python RunScripts.py

(ds_tensorflow) C:\DataScience\SampleNotebooks\Threading>cat null*
2020-07-13 15:46:21.251606
List processing complete.
2020-07-13 15:46:29.130219
2020-07-13 15:46:22.501599
List processing complete.
2020-07-13 15:46:31.227954
2020-07-13 15:46:23.758498
List processing complete.
2020-07-13 15:46:32.431079

You can also use the same idea with functions, if you want to keep the code in once place. This example will run two different functions two times, in parallel.

Function example:

...
import threading
...

def some_function()
    # code

def other_function()
    # code

if __name__ == "__main__":    
    jobs = []
    #same function run multiple times
    threads = 2
    for i in range(0, threads):
        out_list = list()
        thread1 = threading.Thread(target=some_function(size, i, out_list))
        jobs.append(thread1)
        thread2 = threading.Thread(target=other_function(size, i, out_list))
        jobs.append(thread2)
        
    # Start the threads (i.e. calculate the random number lists)
    for j in jobs:
        j.start()

    # Ensure all of the threads have finished
    for j in jobs:
        j.join()

    # continue processing
like image 89
Donald S Avatar answered Apr 16 '26 20:04

Donald S