2

I want to run multiple scripts at once (around 15), i'm assuming I can use the subprocess library to do this, I haven't had any experience with this though.

I'm currently playing around with this script:

import os
from subprocess import *

#run child script 1
p = Popen([r'D:\python\loanrates\test\test.py', "test"], shell=True, stdin=PIPE, stdout=PIPE)
output = p.communicate()
print output

#run child script 2
p = Popen([r'D:\python\loanrates\test\test2.py', "test2"], shell=True, stdin=PIPE, stdout=PIPE)
output = p.communicate()
print output

Each python script it trys to run is simple (for testing purposes)

import time 

for i in range(50):
    print i 
    time.timer(1)

Is this correct ? is this running each script ? It runs with no errors

('', None)
('', None)
[Finished in 0.3s]

If so, what do I need to change in the child scripts so that the parent prints the outputs ? Or similarly what do i need to chnage in the parent so it prints the print functions in the child?

EDIT: the scripts i'm running are in different folders here is a pastebin for the actual scripts , each one differs by the suffix on the API call seen in URL. There are no functions and nothing is return the code is to be run continuously on a while True loop

1 Answer 1

3

If you know the path of your other scripts, you can import them as modules and run their methods

test.py

def main():
    print "Hello, World!"

main.py

import test

test.main()

if you want to execute them at the same time, use threads.

test1.py

def main(name):
    print "Goodbye, %s!"%name

main.py

import test,test1
from threading import Thread

Thread(target=test.main).start()
Thread(target=test1.main,args='user').start()
Sign up to request clarification or add additional context in comments.

1 Comment

Hi thanks for the reply, the problem is the scripts are in other folders elsewhere, the one i supplied was just a test. Second, don't have any functions it's a relatively simple script (to query and API and store data) would syntax out i use for this ? Also it has become apparent that these methods (Thread) only display the output of the script if the script returns it, for instance i could not have it print a print statement i had in the script? The script is on a 'while True' loop btw so there is never something returned. I will paste a copy of the script in a EDIT

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.