Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop twisted reactor on a condition

Tags:

Is there a way to stop the twisted reactor when a certain condition is reached. For example, if a variable is set to certain value, then the reactor should stop?

like image 882
gmemon Avatar asked Jun 29 '11 20:06

gmemon


2 Answers

Ideally, you wouldn't set the variable to a value and stop the reactor, you'd call reactor.stop(). Sometimes you're not in the main thread, and this isn't allowed, so you might need to call reactor.callFromThread. Here are three working examples:

# in the main thread:
reactor.stop()

# in a non-main thread:
reactor.callFromThread(reactor.stop)

# A looping call that will stop the reactor on a variable being set, 
# checking every 60 seconds.
from twisted.internet import task
def check_stop_flag():
    if some_flag:
        reactor.stop()
lc = task.LoopingCall(check_stop_flag)
lc.start(60)
like image 125
Jerub Avatar answered Sep 23 '22 19:09

Jerub


sure:

if a_variable == 0:
    reactor.stop()
like image 7
Gerrat Avatar answered Sep 23 '22 19:09

Gerrat