MistahX
MistahX

Reputation: 736

kill a function after a certain time in windows

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

All I want to do is stop executing a function after X amount of time has elapsed.

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 the difference between "commands, shells, subprocesses, threads, etc.." and this function, which is why I'm having trouble manipulating subprocesses.

I found this code here, and tried it but as you can see it keeps printing after 10 seconds have elapsed:

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')

What's a simple way this function can be killed. As you can see in my attempt above, it doesn't terminate after 20 seconds and just keeps on going...

***OH also, I've read about using signal, but I"m on windows so I can't use the alarm feature.. (python 2.7)

**assume the "infinitely running function" can't be manipulated or changed to be non-infinite, if I could change the function, well I'd just change it to be non infinite wouldn't I?

Here are some similar questions, which I haven't able to port over their code to work with my simple function: Perhaps you can?

Python: kill or terminate subprocess when timeout

signal.alarm replacement in Windows [Python]

Ok I tried an answer I received, it works.. but how can I use it if I remove the if __name__ == "__main__": statement? When I remove this statement, the loop never ends as it did 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')

Upvotes: 6

Views: 10208

Answers (4)

detly
detly

Reputation: 30342

Use the building 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 could also accomplish this with a Pipe/Connection pair, but I'm not familiar with their API. Change the sleep time or TIMEOUT to check the behaviour for either case.

Upvotes: 6

Andrew Skirrow
Andrew Skirrow

Reputation: 3451

Can't you just return from the loop?

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

Upvotes: 1

phihag
phihag

Reputation: 288180

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 in the loop of a thread for an event, which is more portable and flexible as it allows other reactions than brute killing. However, this approach fails if # do stuff can take time (or even wait forever on some event).

Upvotes: 0

Pär Wieslander
Pär Wieslander

Reputation: 28934

There is no straightforward way to kill a function after a certain amount of time without running the function in a separate process. A better approach would probably be to rewrite the function so that it returns after a specified 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

Upvotes: 1

Related Questions