What could be causing python script to loop? - python

Alright, I so here are the facts. I have 2 python scripts and I want Script1 to trigger Script2. I have tried the following ways to do this:
from subprocess import call
call(["python3", "script2.py"])
The dreaded exec call:
exec(open("script2.py").read())
And finally:
os.system("script2.py 1")
So just to make sure I am giving you all the info needed. I want to run script1 first then once it is finished processing I want script1 to trigger script2. Currently no matter what I have tried, I get stuck in a loop where script one, just simply keeps running over and over again.
Any ideas?
Here is the actual code for script1:
import os
"""This looks like it is unnecessary but I can't include its context
in this post. Just know it has an actual purpose."""
input_file = "gs://link_to_audio_file.m4a"
audio = input_file
output_format = os.path.basename(input_file).replace("m4a", "flac")
os.system('ffmpeg -i %s -ar 16000 -ac 1 %s' % (audio,output_format))
os.system("python3 script2.py")

Make sure the first script runs cleanly by itself by commenting out the call to the second script. If it still seems to run forever there's an issue other than trying to call a second script. If you have a IDE, you can step through the code to discover where it hangs. If you're not using an IDE, place print statements in the script so you can see the execution path. Do you possibly have a cyclic call? So the first python script is calling the second and the second python script is in turn calling the first?

When using os.system, I believe you'd need to include python as in
os.system("python script2.py 1")
I can't tell why you're in a loop without seeing the scripts.

I have finally solved this issue! I was actually using an import statement in the second script that was trying to import a variable from the first script, but instead it was importing the entire script, causing it to run in an endless loop. Just like LAS had suggested, nicely done! Thank you all for all your help on this!

Related

Can't get a python file to run 2 other python files on parallel

So I have been trying for hours..DAYS to figure out a code to run 2 python files simultaneously.
I have tried subprocesses, multiprocessing, bash and whatnot, I must be doing something wrong, I just don't know what.
I have 2 python files, and I want to run them in parallel, but note that neither of them end. I want, while the first file is open and running, to run a second file.
Everything I have tried only opens the first file and stops there, since the script there is supposed to be running 24/7. Note that when I tried to use a separate bash file, it, for some reason, opened on git and then closed, doing nothing. I'm really desperate at this point ngl
Please do provide detailed answers with code, as I have been scanning the whole internet (StackOverflow included), I have tried everything and nothing seems to be working..
import subprocess
import LemonBot_General
import LemonBot_Time
import multiprocessing
def worker(file):
subprocess.Popen(["python3 LemonBot_Time.py"], stdout=subprocess.PIPE)
subprocess.Popen(["python3 LemonBot_General.py"],stdout=subprocess.PIPE)
if __name__ == '__main__':
files = ["LemonBot_General.py","LemonBot_Time.py"]
for i in files:
p = multiprocessing.Process(target=worker, args=(i,))
p.start()
This is the latest I tried and didn't work..
I also tried the subprocess commands alone, that didn't work as well.
Bash file also didn't work.
EDIT: NEITHER of the files FINISH. I want to run them in parallel.
You should be able to use Popen from subproccess. Worked for me. If you remove the p.wait() line, the second file will quit as soon as this first file finishes.
import time
import subprocess
p = subprocess.Popen(['python', 'test_file.py'])
time.sleep(5)
print("Working well")
p.wait()
Use the os.system('python3 myprogram.py') command inside of a threading.thread() command for each file.

Print output from python to C# application

I have made a C# app which calls a python script.
C# app uses Process object to call python script.
I also have redirected the sub-process standard output so I can process the output from python script.
But the problem is:
The output(via print function) from python will always arrive at once when the script terminates.
I want the output to arrive in real time while script running.
I can say I have tried almost all of method can get from google, like add flush of sys.out, redirect sysout in python, C# event driven message receiving or just using while to wait message etc,.
How to flush output of print function?
PyInstaller packaged application works fine in Console mode, crashes in Window mode
I am very wondering that like PyCharm or other python IDE, they run python script inside, but they can print the output one by one without hacking original python script, how they do that?
The python version is 2.7.
Hope to have advise.
Thank you!
I just use very stupid but working method to resolve it:
using thread to periodically flush the sys.out, the code piece is like this:
import sys
import os
import threading
import time
run_thread = False
def flush_print():
while run_thread:
# print 'something'
sys.stdout.flush()
time.sleep(1)
in main function:
if __name__ == '__main__':
thread = threading.Thread(target=flush_print)
run_thread = True
thread.start()
# my big functions with some prints, the function will block until completed
run_thread = False
thread.join()
Apparently this is ugly, but I have no better method to make work done .

Start different scripts from script

I would like to have several scripts running on PythonAnywhere. In order to make sure that the scripts are not killed I would like to check for their status in an interval of five minutes (based on https://help.pythonanywhere.com/pages/LongRunningTasks/).
Two questions arise:
1. In the script which runs every five minutes I would like to check whether the other scripts (script2, script3) are still alive or not. If not, I would obviously like to run them. But how do I run several scripts from one script (script1) without script1 getting "stuck"? I.e. how do I start two scripts at the same time from one script?
If I just try to run the script using "import script2" I get an error
ImportError: No module named script2
How do I tell Python that the script is in a different folder (because that has to be the issue)?
Thanks in advance!
Try this:
import time
import subprocess
def check_process(proc,path):
if proc.poll()!=1:
print('%s still running' % proc)
elif proc.poll()==1:#will give a 1 if the child process has been killed
print('%s is dead. Re-running')
subprocess.Popen(['python.exe', path])
script1=subprocess.Popen(['python.exe', pathscript1])
script2=subprocess.Popen(['python.exe', pathscript2])
while True:
check_process(script1,pathscript1)
check_process(script2,pathscript2)
time.sleep(300)

Wait for subprocess .exe to finish before proceeding in Python

I'm running an application from within my code, and it rewrites files which I need to read later on in the code. There is no output the goes directly into my program. I can't get my code to wait until the subprocess has finished, it just goes ahead and reads the unchanged files.
I've tried subprocess.Popen.wait(), subprocess.call(), and subprocess.check_call(), but none of them work for my problem. Does anyone have any idea how to make this work? Thanks.
Edit: Here is the relevant part of my code:
os.chdir('C:\Users\Jeremy\Documents\FORCAST\dusty')
t = subprocess.Popen('start dusty.exe', shell=True)
t.wait()
os.chdir('C:\Users\Jeremy\Documents\FORCAST')
Do you use the return object of subprocess.Popen()?
p = subprocess.Popen(command)
p.wait()
should work.
Are you sure that the command does not end instantly?
If you execute a program with
t = subprocess.Popen(prog, Shell=True)
Python won't thrown an error, regardless whether the program exists or not. If you try to start an non-existing program with Popen and Shell=False, you will get an error. My guess would be that your program either doesn't exist in the folder or doesn't execute. Try to execute in the Python IDLE environment with Shell=False and see if you get a new window.

How do I start a subprocess in python and not wait for it to return

I'm building a site in django that interfaces with a large program written in R, and I would like to have a button on the site that runs the R program. I have that working, using subprocess.call(), but, as expected, the server does not continue rendering the view until subprocess.call() returns. As this program could take several hours to run, that's not really an option.
Is there any way to run the R program and and keep executing the python code?
I've searched around, and looked into subprocess.Popen(), but I couldn't get that to work.
Here's the generic code I'm using in the view:
if 'button' in request.POST:
subprocess.call('R CMD BATCH /path/to/script.R', shell=True)
return HttpResponseRedirect('')
Hopefully I've just overlooked something simple.
Thank you.
subprocess.Popen(['R', 'CMD', 'BATCH', '/path/to/script.R'])
The process will be started asynchronously.
Example:
$ cat 1.py
import time
import subprocess
print time.time()
subprocess.Popen(['sleep', '1000'])
print time.time()
$ python 1.py
1340698384.08
1340698384.08
You must note that the child process will run even after the main process stops.
You may use a wrapper for subprocess.call(), that wrapper would have its own thread within which it will call subprocess.call() method.

Categories

Resources