Python subprocess running out of order on Windows - python

I'm running a python script on Windows.
I have a python script like this:
subprocess.call(1)
subprocess.Popen(2)
subprocess.call(3)
when I run the script, the results I get runs like this:
subprocess.call(3)
subprocess.call(1)
subprocess.Popen(2)
Why is this happening?

Each new process you create with subprocess spawns a new sub-process, hence its name. This means the commands will finish running at different times meaning you get the results in a different order.
It is not the same as calling a function in Python, where the function finishes running before the others are ran.

Related

Can we launch a parallel shell command from python?

I want to use a go executable: timescaledb-parallel-copy to insert data into database from a csv file. However, I plan to use Python for reading in the filename and lookup the appropriate table name for insertion. If I then launch timescaledb-parallel-copy as a Python subprocess to execute on shell, will it still be parallel? I do not need Python to make it parallel, it is parallel by default. I just do not want Python to make it single-threaded.
If you are using subprocess.run() then your program, timescaledb-parallel-copy will execute as if you had called it from the shell. It will still be in parallel. The python script will not be, and will wait on timescaledb-parallel-copy to return.
Yes, I believe it will be. By launching the program as a subprocess, you are running the program as it originally would be, with no interference from python.

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.

Using os.system() in Python is open a program, can't see window of launched program

I am trying to launch a program/GUI from within a python code.
From the terminal, I can get the program to launch by simply typing the program name. A few lines get outputted to the terminal, and then a separate window opens with the GUI.
I tried to emulate this in python by running
os.system("<program name>")
The typical output lines, as mentioned above, get printed to the console, but no window opens up with the GUI.
Can os.system() be used to execute programs that have their own separate window?
From the Python manual:
[os.system] is implemented by calling the Standard C function
system()
That being said, you shouldn't have any problems launching a GUI application with os.system. I've just tried it myself and it works fine.
It also mentions in the manual that:
The subprocess module provides more powerful facilities for spawning
new processes and retrieving their results; using that module is
preferable to using this function.
Maybe that's worth a try. Do any other GUI applications work when you spawn them with os.system?
Here is a solution using subprocess
import subprocess
subprocess.Popen("notepad.exe")
Or if you want to run a python program with a specific interpreter:
subprocess.Popen('{0} {1}'.format(PythonInterpreterPath,PythonFilePath.py))

Open another Python script from another Python process

I'm trying to open a python script from the main python program in a separate process.
For now let's just say that "main program" is a PyQt4 GUI program and "script" is the script (in a separate file) I am trying to run from my main program.
Why?
So the script keeps running after the main program is closed
So that when the script is ran my main program doesn't freeze while waiting for the script with an infinite loop to end.
I know that subprocess.Popen(), subprocess.call() and os.system() can open the file via the command line, but when they open a script with an infinite loop the main program hangs and crashes.
I also know that I could use QtCore.QCoreApplication.processEvents() to keep the main program running, but this does not work in my case.
So I figured the best solution to keep the script and the Main Program correctly running is the have separate processes.
How would I open script.py file in a separate process or in a way that would not freeze up my program.
Calling an external command in Python is probably what you're looking for. The author perfectly describes how to launch different python script that keeps running when your main program is closed.
Don't run python scripts as subprocesses, import the corresponding modules and call the desired functions instead. If you need to run Python code in a separate process, you could use multiprocessing:
multiprocessing.Process(target=infinite_loop, args=['arg 1', 2]).start()
Related: Call python script with input with in a python script using subprocess.
To avoid "freezing" your GUI, do not call functions that block for long in your GUI thread. Either use threads or async. API (here's tkinter code example that uses createfilehandler() and .after() calls, to read output from a subprocess without blocking the GUI).
Popen() only starts a child process and it does not wait for it to exit. If Popen() "freezes" your program something else is broken. Here's code example that starts/stops a tkinter progressbar on start/end of a subprocess.
Qt has its own API: QThread, QProcess, signals that you could use to run a subprocess. Related: How to do stuff during and after a child process.

Python script waiting for some program being launched and then starting another program. (Windows)

I would like to write a python script that will finally be converted to .exe (with pyinstaller lets say) and added to windows startup applications list. This program (once launched) should 'monitor' user and wait until user will start a specified program (say program1.exe) and then my python program should launch another specified program (program2.exe).
I know that there is something like subprocess to launch another program from python script but I was not able to make it work so far ;/ And as it comes to this part where I need to monitor if user does start specified program I have no idea haw to bite on this. I don't expect a complete solution (although it would be very nice to find such ;p) but any guides or clues would be very helpfull.
For the monitoring whether or not the user has launched the program, I would use psutil: https://pypi.python.org/pypi/psutil
and for launching another program from a python script, I would use subprocess.
To launch something with subprocess you can do something like this:
PATH_TO_MY_EXTERNAL_PROGRAM = r"C:\ProgramFiles\MyProgram\MyProgramLauncher.exe"
subprocess.call([PATH_TO_MY_EXTERNAL_PROGRAM])
If it is as simple as calling an exe though, you could just use:
PATH_TO_MY_EXTERNAL_PROGRAM = r"C:\ProgramFiles\MyProgram\MyProgramLauncher.exe"
os.system(PATH_TO_MY_EXTERNAL_PROGRAM)
Hope this helps.
-Alex

Categories