Python built exe process to kill itself after a period of time - python

How is it possible to get a compiled .exe program written in Python to kill itself after a period of time after it is launched?
If I have some code and I compile it into an .exe, then launch it and it stays in a 'running' or 'waiting' state, how can I get it to terminate after a few mins regardless of what the program is doing?
The reason why is that the exe that is launched envokes a URL using PAMIE and automates some clicks. What I have noticed is that if the browser is closed the process remains in memory and does not clean itself up. I wanted to find a way to auto-clean up the process after say 5 mins which is more then enough time. I've tried using psutils to detect the process but that does not work in my case. Any suggestions is greatly appreciated.

def one(Time):
time.sleep(Time)
sys.exit(0)
def two():
thread.start_new_thread(one, (600,)) #10min countdown..
#Your running function here....Will run sim. with function one. when time ends,
#sys.exit exits program..(Not tested:) )
two()

Create a thread when your process starts.
Make that thread sleep for the required duration.
When that sleep is over, kill the process.

Related

How to make the Python subprocess wait for some input when running through SLURM script?

I am running some Python code using a SLURM script on a remote server accessed through SSH. At some point, issues related to licenses on the SLURM platform may happen, generating errors in Python and ending the subprocess. I want to use try-except to let the Python subprocess wait until the issue is fixed, after that it can keep running from where it stopped.
What are some smart implementations for that?
My most obvious solution is just keeping Python inside a loop if the error occurs and letting it read a file every X seconds, when I finally fix the error and want it to keep running from where it stopped, I would write something on the file and break the loop. I wonder if there is a smarter way to provide input to the Python subprocess while it is running through the SLURM script.
One idea might be to add a signal handler for signal USR1 to your Python script like this.
In the signal handler function, you can set a global variable or send a message or set a threading.Event that the main process is waiting on.
Then you can signal the process with:
kill -USR1 <PID>
or with the Python os.kill() equivalent.
Though I do have to agree there is something to be said for the simplicity of your process doing:
touch /tmp/blocked.$$
and your program waiting in a loop with a 1s sleep for that file to be removed. This way you can tell which process id is blocked.

how to check/trace why a python script delays to exit

I have a python script that does some network stuff with the requests library. My issue is that for some reason the python script doesn't seem to exit immediately after it finishes.
I have measured the execution call of the function I use and of the total execution of the script with time.perf_counter().
--ipTest completed in 134.34 sec
--Program completed in 134.34 sec
But the script stops (writes on the file the output and exits) after 260 seconds. This is not always the case though, sometimes this is happening.
Is there a way to check what the script is doing the rest of the time?
Thanks!

Wait and complete processes when Python script is stopped from PyCharm console?

Basically I am writing a script that can be stopped and resumed at any time. So if the user uses, say PyCharm console to execute the program, he can just click on the stop button whenever he wants.
Now, I need to save some variables and let an ongoing function finish before terminating. What functions do I use for this?
I have already tried atexit.register() to no avail.
Also, how do I make sure that an ongoing function is completed before the program can exit?
Solved it using a really bad workaround. I used all functions that are related to exit in Python, including SIG* functions, but uniquely, I did not find a way to catch the exit signal when Python program is being stopped by pressing the "Stop" button in PyCharm application. Finally got a workaround by using tkinter to open an empty window, with my program running in a background thread, and used that to close/stop program execution. Works wonderfully, and catches the SIG* signal as well as executing atexit . Anyways massive thanks to #scrineym as the link really gave a lot of useful information that did help me in development of the final version.
It looks like you might want to catch a signal.
When a program is told to stop a signal is sent to the process from the OS, you can then catch them and do cleanup before exit. There are many diffferent signals , for xample when you press CTRL+C a SIGINT signal is sent by the OS to stop your process, but there are many others.
See here : How do I capture SIGINT in Python?
and here for the signal library: https://docs.python.org/2/library/signal.html

PSUtil and Wine: Trying to get time a process ends, but Wine processes never terminate (just go to sleep)?

I am using Python's PSUtil library to detect the exact time a given process completes. Currently I do this by looking for when the process terminates, for which the following suite of tests works well on both Windows and regular Linux programs:
# Run a command and keep a reference
if do_run_command:
process = psutil.Popen(command)
# Latch onto running process by PID
else:
process = psutil.Process(int(pid))
# Loop until the process ends
while True:
if not process.is_running():
break
if process.status() == psutil.STATUS_ZOMBIE:
break
try:
# Command that gets information about the process
except psutil.NoSuchProcess:
break
# Store end time
However, when I try to use this on a program being run under Wine, none of those tests ever triggers. This is because it appears Wine processes never actually terminate, but rather go to sleep indefinitely. That includes the program running in Wine, the script that launches the program in Wine, etc. etc.
This is causing problems both in being able to detect when the programs truly end, and because the processes build up in memory until I eventually have to kill them manually.
-=-=-=-=-
I am running on CentOS 6.0. The main questions I have boil down to these:
Is there some sort of configuration that can be set for Wine to have
it close the processes when complete? (I am rather new to Wine, and searching for this has proved maddeningly difficult)
Alternatively, is there another PSUtil test I could add to my Python script to get it to detect when these Wine processes are finishing (even if they are sleeping indefinitely)?

How to identify the cause in Python of code that is not interruptible with a CTRL +C

I am using requests to pull some files. I have noticed that the program seems to hang after some large number of iterations that varies from 5K to 20K. I can tell it is hanging because the folder where the results are stored has not changed in several hours. I have been trying to interrupt the process (I am using IDLE) by hitting CTRL + C to no avail. I would like to interrupt instead of killing the process because restart is easier. I have finally had to kill the process. I restart and it runs fine again until I have the same symptoms. I would like to figure out how to diagnose the problem but since I am having to kill everything I have no idea where to start.
Is there an alternate way to view what is going on or to more robustly interrupt the process?
I have been assuming that if I can interrupt without killing I can look at globals and or do some other mucking around to figure out where my code is hanging.
In case it's not too late: I've just faced the same problems and have some tips
First thing: In python most waiting apis are not interruptible (ie Thread.join(), Lock.acquire()...).
Have a look at theese pages for more informations:
http://snakesthatbite.blogspot.fr/2010/09/cpython-threading-interrupting.html
http://docs.python.org/2/library/thread.html
Then if a thread is waiting on such a call, it cannot be stopped.
There is another thing to know: if a normal thread is running (or hanged) the main program will stay indefinitely untill all threads are stopped or the process is killed.
To avoid that, you can make the thread a daemon thread: Thread.daemon=True before calling Thread.start().
Second thing, to find where your program is hanged, you can launch it with a debugger but I prefer logging because logs are always there in case its to late to debug.
Try logging before and after each waiting call to see how much time your threads have been hanged. To have high quality logs, uses python logging configured with file handler, html handler or even better with a syslog handler.

Categories

Resources