CPU occupation while using python subprocess module - python

I wrote a c++ program a.out and tried to run the program by using subprocess module in python, called run.py. After running the script, I used top to check cpu usage and found that the run.py does not shown in the list while a.out is running.
What happened to run.py? Where is it?

It is sleeping waiting for your C++ program to exit. That is because you used subprocess.call.
You can use subprocess.Popen which doesn't wait unless you call its wait method if you need your Python program to continue doing something else while the program runs.

Related

Running a multithreaded C program via python subprocess

I have a multi-threaded C program, which runs perfectly fine as a standalone program. It uses pthread_create and pthread_join to execute some some logic. Now, I am trying to execute this code from python using subprocess. However, when executing via subprocess, it seems subprocess returns as soon as the main thread exits, but I wish to wait for the entire code to finish executing. Is it possible to do this?

Waiting for GNU Parallel processes to finish when using & and spawning via Python

In my current application, I have a Python 2.7 script called main.py that launches another Python 2.7 script called calculator.py using GNU Parallel like the following:
os.system("seq 10000 | parallel -N0 -j 50 nohup python calculator.py &")
print "Done"
This works pretty well, with one exception: I need to resume executing other commands in main.py (that is, after the os.system call, e.g. the print "Done" line) just after all the 10000 instances spawned with GNU Parallel finish running.
Is there a proper way to do that? Solutions with os.spawn and Python 2.7 subprocess are both welcome, but using GNU Parallel is absolutely mandatory.
EDIT: Here are my requirements:
1) it is crucial to me that the many instances of calculator.py that are spawned keep running if the terminal closes (hence the nohup)
2) I need it to not block current terminal session (hence the &)
3) I need it to print "Done" in the example above gets executed only after the 10000 jobs finish
If achieving all above at the same time is not possible, I think I could then manually keep a log of all launched processes and then manually force the rest of the code "main.py" code to continue after all those processes end. This, of course, is a cumbersome last-resource option.

Python script never ends in task scheduler

I am trying to set up a python code to be executed automatically.
I started with a small code to be executed:
import datetime
with open("out.txt","a") as f:
f.write(datetime.datetime.now().isoformat())
The task will start allright, and executes (The file is modified), but it never ends in the task scheduler.
this and this exist in SO, but have no real answer. The only workaround proposed in these threads is to force the end of task after a given time in Windows, but this requires to know how long the python script will take which will not be the case for my actual task.
How can the task scheduler know that a python script is finished ?
I run it the following way in the task scheduler :
program : cmd
arguments : /c C:\python27\python.exe C:\path\of\script.py
execute in : C:\path\of\
I tried some variations around this, like executing python instead of cmd, but it didn't change anything. I had hoped the /c would force the task to close.
as Gaurav Pundir mentioned, adding sys.exit(0) should end the script properly and thus the task. However, you do need to add the sys library with import sys in order to use sys.exit(0). Hope this helps!
it looks like a bug to me.
Try looking up for python console under Task Manager.
if it is not there then the program has exited successfully.
I have the same issue with Windows 10, python script ran successfully, there is no python console under Task Manager, yet the scheduled task Status still says 'Running'
There seems to be no correct fix for such issue with CMD as the intermediate launcher.
There is a [End] command in Task Scheduler GUI, clicking it will always terminate the CMD/batch file leaving the spawned python.exe process to straw.
The real problem: there doesn't seem to be any way for cmd to pass on the terminate signal to python.exe.. and neither can taskengine reliably determine if python.exe is alive or not.
I ran into the same problem, the python file didn't stop in the task scheduler. I imported sys and wrote sys.exit(0) but I still got the same problem.
Finally, I decided to press "Update" which solved my problem; the status of the task was "Ready", and not "Running". For information, I use windows 11.

os.system('exit') in python

My friend is in a macOS environment and he wanted to call os.system('exit') at the end of his python script to make the terminal close. It doesn't. This doesn't surprise me but I would like to know what exactly is going on between the python script and the terminal when this call is made.
In my mental simulation the terminal should have to tell you that there are still running jobs, but that doesn't happen either.
As a side question : will some less common terminals close when a process calls this?
read the help:
Execute the command (a string) in a subshell.
A subshell is launched, and exit is run in that subshell.
To exit the enclosing terminal, you have to kill the parent. One way to do it is:
os.system("kill -9 %d"%(os.getppid())
The system function starts another shell to execute a command. So in this case your Python scripts starts a shell and runs "exit" command in there, which makes that process exit. However, the Python script itself, including a terminal where it is running, continues to run. If the intent is to kill the terminal, you have to get the parent process ID and send a signal requesting it to stop. That will kill both Python script and a terminal.
Remember that system first spawns/forks a sub-shell to execute its commands. In effect, you are asking only the sub-shell to exit.

How to call an executable as independent process using python in windows

After calling an exe using python script in windows, the exe should run independent of this python script and once it is initiated the control should comeback to python script and executes the further script and control of .py file will die. But on other side before finishing execution, the exe should call this python script.
Ideas would be highly appreciated.
I have tried following commands:
os.system("start test.exe")
os.startfile("test.exe")
os.spawnlv(os.P_NOWAIT, "test.exe")
os.spawnv(os.P_NOWAIT, 'C:\Python31\python.exe', ('python', 'test.py'))
os.execvp("python3", ("test.py", ))
I sounds as if you want the callee to callback the caller (sorry for the alliteration :) Since you are using Python 3.1 maybe the subprocess module will provide the intended behavior. It is not a true callback per se, but the calling program can perform decisions based on the output of the called program (exe in this case.)

Categories

Resources