Python kill scripts - python

I need to kill all python scripts except one. Unfortunattely, all scripts have similar name "pythonw.exe". Difference in PID only.
First time, i don't need to leave one script alive, thats why i just kill all python scripts in system by taskkill /F /T /IM "python*" command.
But now, i have one script, that automates all other scripts. The script is my simple "testing system". It rewrite object script, start, stop and restart.
But my problem is multithreading in objective script (10 - 20 threads).
I don't know, how to kill all python threads, except automates one.
P.S.
I tried to get tasklist and PID of automates script, and killed all scripts, except that one, but it doesn't work (i don't know why)
P.P.S
OS: Windows XP
Python 2.7.8

My solution is place testing code in exe file. Now i can kill all python scripts, as previously. Maybe someone will offer another solution?

Related

Finding details of externally-launched Python process

I'm on Windows 7. I have a program that launches a Python script using the system Python interpreter. The process quickly finishes. I want a way to see the process, its command line arguments, and any other information about it. (PID?)
How can I do this? The process is killed before I could open Process Explorer. I can cause the program to launch the Python script whenever I want.

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.

Find Windows PID of a python script with Windows Command Prompt

I am running two different python scripts running on a windows machine simultaneously and would like to kill one but not the other from the command prompt. Using taskkill with the name "python.exe" does not allow me to choose to kill just one of these scripts.
Is there a way in windows to kill just one of these tasks, determined by the script from which it originated?
For example: if I run python_process1.py and python_process2.py and would like to kill the .exe associated with just python_process2.py and leave python_process1.py alone.
UPDATE: the solution below does not kill the process, and the issue still lies in identifying the PID of a process by python script name. If this is impossible, is there a way to selectively kill python scripts on windows that I am unaware of?
Thank you.
Using Get-WmiObject and PowerShell you can examine the command line arguments for each process, then pass the selected Process ID to taskkill.
This example shows how to kill a process executing the script test.py:
PS C:\Users\Administrator> taskkill.exe /F /PID $(Get-WmiObject Win32_Process -Filter "name = 'python.exe'" | Where-Object {$_.CommandLine -like '*.\test.py'} | Select -ExpandProperty ProcessId)
Modify *.\test.py to match how you are actually calling each script, and it should work for you

How to free Ubuntu memory once python script is stopped from terminal (Ctrl-C)?

I was calling a python script (2.7) from a console (Ubuntu 14.04) using a command: python script_name.py. At some point, I wanted to stop the running script by pressing Ctrl-C. However, when I checked Ubuntu System Monitor, the memory used by the python script was not freed up (I monitored Ubuntu System Monitor before I called the script, during the process, and after I pressed Ctrl-C to stop the script). I tried to free up the memory using a command explained on http://www.upubuntu.com/2013/01/how-to-free-up-unused-memory-in.html , but didn't work (I mean, the memory usage was not changed).
However, if I used pycharm to run and stop the script, the memory was freed up directly once I pressed the Stop button. For some reasons (such as from ssh or just to test from console), I want to run my script from the console (without using pycharm or any other IDEs).
My question is, what is the command, or how to stop running python script and free up directly the memory used by the script, if I run the script from the console?
Many thanks in advance.
Those commands did not work since what you're trying to achieve is not what they do. How did you check the memory being used by your Python script. I use top to see memory and could used by each process (sorted in ascending order by default). You may have checked before the system had time to register that the python process was killed, I've used this a lot and I've never tun into with the OS not getting memory back after a process has been killed with ctrl + c.
Pycharm is probably doing some cleanup when you stop the program from it versus just having to wait for the OS to reclaim memory versys when you SIGTERM a process from a shell

How do you terminate the Python shell in Wing IDE?

I'm fairly new to Python and have been using Wing IDE to play around with the features. One of the things that I could find while looking around was how to force terminate the Python shell when executing a command that won't terminate any time soon. An example would be:
import math
math.factorial(1000000)
I know in Visual Studio C++, the command is Ctrl+C, but what exactly is the Python equivalent?
The method used to terminate execution varies between shells. For Wing IDE you use the Restart Shell item on the Options menu.
This depends on your shell. For most shells, it is ctrl-C, or killing the process.
There is no way to do so from within python (unless you are spawning threads or processes) because the thread in question is stuck.

Categories

Resources