Kill a function after a certain time in windows

I read a lot of posts about using threads, subprocesses, etc. A lot seems complicated for what I'm trying to do ...

All I want to do is stop the function after X time.

def big_loop(bob): x = bob start = time.time() while True: print time.time()-start 

This function is an endless loop that never throws any errors or exceptions, period. I'm not sure about the difference between "commands, shells, subprocesses, threads, etc." And this function, so I am having problems managing subprocesses.

I found this code here and tried it, but as you can see, it continues to print after 10 seconds:

 import time import threading import subprocess as sub import time class RunCmd(threading.Thread): def __init__(self, cmd, timeout): threading.Thread.__init__(self) self.cmd = cmd self.timeout = timeout def run(self): self.p = sub.Popen(self.cmd) self.p.wait() def Run(self): self.start() self.join(self.timeout) if self.is_alive(): self.p.terminate() self.join() def big_loop(bob): x = bob start = time.time() while True: print time.time()-start RunCmd(big_loop('jimijojo'), 10).Run() #supposed to quit after 10 seconds, but doesn't x = raw_input('DONEEEEEEEEEEEE') 

In what a simple way this function can be killed. As you can see in my attempt above, it does not end after 20 seconds and just continues ...

*** OH too, I read about using the alarm, but I'm on the windows, so I can’t use the alarm function. (python 2.7)

** suppose that an "infinitely executing function" cannot be changed or changed without infinity, if I could change the function, well, I would just change it to infinity, right?

Here are some similar questions that I couldn’t move through my code to work with my simple function: Perhaps you can?

Python: kill or terminate a subprocess when timeout

replacing signal.alarm on Windows [Python]

Ok, I tried to get the answer, it works. But how can I use it if I delete the if __name__ == "__main__": ? When I delete this statement, the cycle never ends as before.

 import multiprocessing import Queue import time def infinite_loop_function(bob): var = bob start = time.time() while True: time.sleep(1) print time.time()-start print 'this statement will never print' def wrapper(queue, bob): result = infinite_loop_function(bob) queue.put(result) queue.close() #if __name__ == "__main__": queue = multiprocessing.Queue(1) # Maximum size is 1 proc = multiprocessing.Process(target=wrapper, args=(queue, 'var')) proc.start() # Wait for TIMEOUT seconds try: timeout = 10 result = queue.get(True, timeout) except Queue.Empty: # Deal with lack of data somehow result = None finally: proc.terminate() print 'running other code, now that that infinite loop has been defeated!' print 'bla bla bla' x = raw_input('done') 
+7
source share
4 answers

Use the standard blocks in the multiprocessing module:

 import multiprocessing import Queue TIMEOUT = 5 def big_loop(bob): import time time.sleep(4) return bob*2 def wrapper(queue, bob): result = big_loop(bob) queue.put(result) queue.close() def run_loop_with_timeout(): bob = 21 # Whatever sensible value you need queue = multiprocessing.Queue(1) # Maximum size is 1 proc = multiprocessing.Process(target=wrapper, args=(queue, bob)) proc.start() # Wait for TIMEOUT seconds try: result = queue.get(True, TIMEOUT) except Queue.Empty: # Deal with lack of data somehow result = None finally: proc.terminate() # Process data here, not in try block above, otherwise your process keeps running print result if __name__ == "__main__": run_loop_with_timeout() 

You can also accomplish this with a Pipe / Connection pair, but I am not familiar with their API. Change the timeout or TIMEOUT to check the behavior for any occasion.

+6
source

There is no easy way to kill a function after a certain time without starting the function in a separate process. It would be best to rewrite the function so that it returns after a certain time:

 import time def big_loop(bob, timeout): x = bob start = time.time() end = start + timeout while time.time() < end: print time.time() - start # Do more stuff here as needed 
0
source
 import os,signal,time cpid = os.fork() if cpid == 0: while True: # do stuff else: time.sleep(10) os.kill(cpid, signal.SIGKILL) 

You can also check the thread loop for an event that is more portable and flexible, as it allows for other reactions besides gross killings. However, this approach fails if # do stuff can take some time (or even wait a long time for some event).

0
source

Can't you just get back out of a loop?

 start = time.time() endt = start + 30 while True: now = time.time() if now > endt: return else: print end - start 
0
source

All Articles