Start two infinite Python scripts from GUI on Windows - python

I'm trying to start two Python scripts that will run in an infinite loop.
The first script scrapes a webpage and dumps it into a CSV file.
The second script reads that CSV file and displays it on a webpage through Dash (localhost webserver).
With everything I've tried so far, it will run one script and wait for it to end before running the next (which doesn't work for me).
The only thing that has worked for me so far (which isn't optimal for production) is opening two Command Prompts and manually running each script in separate windows.
I've tried two buttons in PyQt.
I've tried a simple batch script (I'm on a Windows7 machine) with structure:
python file1.py &
python file2.py &
The functionality I need:
"Whilst the first script is scraping and dumping the web page, the other is at the same time reading and displaying it."

You can use start to launch both commands:
#echo off
start /b python file1.py
start /b python file2.py
A few notes about this: The commands will launch, but will not block the exit of the batch file, so unless you'll need to keep the command prompt open. Also, to kill them, use Ctrl-Break to stop the background processes.
It might be cleaner to use Python's multiprocessing to run the scripts, either as part of one of the scripts, or with a wrapper script, but that depends on your exact needs.

So you want to run file2.py after file1.py is completely finished its job?
If so, you can replace & with && so it would be something like this:
python file1.py && python file2.py

Related

Run python script from python script BUT outside of python script

It sounds like riddle or joke but actually I havent found answer to this problem.
What is actually the problem?
I want to run 2 scripts. In first script I call another script but I want them to continue parallely and not in 2 separate threads. Mainly I dont want 2nd script to be running inside 1st python script(That means if I run Chrome Browser from python script and then shut down the python script, the Chrome will be shut down too).
What I want is like on Linux machine: I open two terminals and run both scripts in each terminal - They are not two threads, they are independent on each other, shutting one will NOT shut down the other. Or it can be like on Linux machine where I can run 2 python scripts in terminal behind background with 'python xxx.py &' (&) symbol.
Summary:
I would like to run inside 'FIRST.py' script 'SECOND.py' script. However not with threading module and mainly have SECOND.py script independent on FIRST.py script, that is, shutting down FIRST.py will not have any consequence on SECOND.py.
THE SOLUTION SHOULD BE WORKING ON WINDOWS, LINUX AND MAC.
BTW:
I tried on windows:
subprocess.call(['python','second.py','&'])
subprocess.call(['python','second.py'])
os.system('python second.py') # I was desperate
They run serially, so first.py script is blocked untill second.py finishes.
I havent try Threading with daemon=False but I feel its kind of Demon and I dont feel my skill is that far that I can control threads existing outside of my playground :)
Thanks in advance for help
You can use the Popen constructor from the subprocess module to launch background processes, using
import subprocess
p = subprocess.Popen(["python","second.py"])
creates a background process and execution of first.py is not blocked.

Open terminal, run python script and keep it open for results?

How to get an sh script for starting a new terminal, execute a python script and keep it running? The python script is supposed to run continuously in a perpetual loop, spitting out results as they pop in. Whenever trying with sh-script for gnome-terminal just getting: child process exited normally with status 2
Manually it would just be: python home/ubuntu/pyscript.py
Could someone give an idea how to do this?
I have a list of scripts to run, so resorting to the manual solution is tedious.
You can use gnome-terminal with the -x flag.
Suppose you have a spam.py script; then the following command will spawn a new terminal, run spam.py in it, and close the terminal once the script has ended.
gnome-terminal -x python spam.py
Try with this script:
# spam.py
import time
for _ in range(5):
print("eggs")
time.sleep(1)
Then the previous command will spawn a terminal, that will be printed eggs five times, and then will be closed.
If you want to leave the terminal open with the Python interpret still running after the script ended, then Python's -i flag (doc then CTRL+F -> -i) is what you want:
gnome-terminal -x python -i spam.py
To run the Python script in a new instance of your favourite terminal, write:
x-terminal-emulator -e python -i home/ubuntu/pyscript.py
This will start the Python script and run it until it ends, then display a Python prompt to stop the terminal emulator from closing.
This will work with x-terminal-emulator substituted with any of the many, many terminals installed on my computer, so will work with little modification across all POSIX-compatible systems with the standard terminals installed. This won't work on a Mac, however. For a properly cross-platform Python implementation of something slightly different, see here. Most of the techniques should be transferable.
To run the Python script in the same terminal whilst carrying on with the rest of the shell script, write:
python home/ubuntu/pyscript.py &
Note the &, which runs the program as a new process (but still connects the output to the virtual terminal).

Batch Rendering file from a python script without openeing Maya

I have one Maya scene and a Python script where import obj files into it. I need to create a batch render file which calls the maya file and applies the script without opneing maya.
I have this code in a .sh file:
#!/bin/bash
"/Applications/Autodesk/maya2016/Maya.app/Contents/bin/Render" -r file -s 1 -e 4 -cam camera1 -rd "/Users/MyComp/Documents/maya/projects/default/images" "/Users/MyComp/Documents/maya/projects/default/Scenes/test1.mb"
But I have this code into the script which can be an issue or maybe not:
def renderFile(i):
cmds.setAttr("defaultRenderGlobals.imageFilePrefix", i, type="string")
cmds.render(batch=True)
If I execute this .sh file it renders without the python script. How can I add the python script?
I need that file for a renderfarm purposes
I know it's an old thread but thought I'd jump in just incase someone finds this thread in a search.
The comments seem a little confused. This comes from the fact that there are two different Python interpreters being talked about. The first is the system level one, which the original question seems to be talking about. In that case, you can use any of the various shell command launchers (like, subprocess/Popen) that suit your need. Here you are looking to run the render command like you would any other command in in the shell.
In the responses, people there are referring to the other interpreter, the custom Maya Python interpreter (mayapy.exe). In that case you are working with actual Maya libraries and it's the same as working with Python in it's shell, with the added Maya libraries/environment.
The two have different uses, the first is to control things like they were in the shell and the second is controlling things inside of a Maya context. Hope that clarifies things.

Call bash script after exiting python program

I'm new to Python and have been able to find answers to most of my questions here so I thought you guys could help me with this one:
I'm developing somewhat of a hybrid application using bash scripts to show menus and to call python applications (I'm doing it this way to simplify things).
My problem is that when I end the python application, it simply terminates the process and to go back to the menus, I have to start the whole program again. I tried using "subprocess.call('xxx')" but it opens the bash scrips inside of the application I am running and shows text (echo) only, no other functions.
Is there a way to end the python application first and then call the shell script?
You can wrap your code in a while-true loop
while :; do
# Assuming you want to show the menu before you start a program:
bash showMenu.py
python myScript.py
# When the above scripts exits the process will start all over again
# You might want to consider checking the exit code and only continue if the program exits with status code 0
# [ $? gt 0 ] && break
done

How to name / find a specific running python script process on windows?

On windows:
I have two scripts:
Client.py
Console.py
Q1:
Client can be run multiple times but only single instance should be left running. It is being run with pythonW.exe
Q2:
Multiple console.py can be run at the same time. But the last one that closes should kill the client.py
Limitations:
Strongly preferred not to write any files. (i.e. use search by window name, PID etc...)
Strongly preferred not to install any additional modules for python. i.e. use ctypes etc...
For Q1 i tried to do ctypes.windll.user32.SetWindowTextA ("NAME") and then search for it. it works for python.exe but not for pythonW.exe beacause there is no console window then.
Thanks!

Categories