I have an custom object which has a long running function. I want to be able to tell the object to stop running the method without killing it, e.g. so that it waits again for a signal to resume its work.
Code: Select all
class Foo(object):
def __init__(self):
self.msg = ''
def do(self):
for x in xrange(0, 100000):
if self.msg == 'stop':
print 'Foo received stop. Terminating'
return
print 'Current x: {}'.format(x)
time.sleep(3.0)
FOO = Foo()
def stopFoo():
FOO.msg = 'stop'
Now, somewhere down the line, someone sends an HTTP request telling FOO to run
//FOO.do() got called somewhere
Now, someone wants to tell FOO to stop running. In another HTTP request, they do this:
//FOO.msg = 'stop'
However, I observe that FOO.do() continues to execute! And the FOO.msg = 'stop' keeps blocking.
How can I change FOO's field while its running, such that it will stop in a controlled manner?