kill a function after a certain time in windows
<p>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...</p>

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

<pre><code>def big_loop(bob):
    x = bob
    start = time.time()
    while True:
        print time.time()-start
</code></pre>

<p>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.</p>

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

<pre><code>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')
</code></pre>

<p>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...</p>

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

<p>**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?</p>

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

<p><a href="http://stackoverflow.com/questions/4158502/python-kill-or-terminate-subprocess-when-timeout">Python: kill or terminate subprocess when timeout</a></p>

<p><a href="http://stackoverflow.com/questions/644073/signal-alarm-replacement-in-windows-python">signal.alarm replacement in Windows [Python]</a></p>

<p>Ok I tried an answer I received, it works.. but how can I use it if I remove the <code>if __name__ == "__main__":</code> statement? When I remove this statement, the loop never ends as it did before..</p>

<pre><code>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')
</code></pre>
