Reputation: 76
An application, completely scheduled and monitored by some Job Monitor, implements a JMSMessageListener... now what is the best way to prevent this main thread (which is registering itself on JMS Queue) not to finish it's execution. Otherwise Job monitor will mark this job as completed while on message event thead was supposed to work instead.
There are 2 ways-
1st way
import timer
timer.sleep(_LONG_TIME) # this will stop it from leaving execution
2nd way-
from twisted.internet import reactor
reactor.run() # this will send execution control to reactor
So now as I understood this... sleeping a thread will not impact CPU and machine resources. While I am not that sure what's happening, once control goes to reactor ?
Upvotes: 1
Views: 70
Reputation: 11347
Neither will affect the CPU significantly. The python reactor is based off the ractor pattern, at it's core is an event loop with blocks until an event arrives and then handles it. In the trivial case there are no events provided so no work is ever done.
It might be worth checking out the Twisted reactor documents. Start here: http://twistedmatrix.com/documents/13.1.0/core/howto/reactor-basics.html#auto0
With the twisted reactor you could provide an 'exit' ability by changing the code to post an event that caused the reactor to finish. Your main thread would only do work when this event was posted. With the sleep solution you'd have to sleep for a small interval, check to see if the exit event was posted, then go back to sleep if it wasn't - this mean that exit was not immediate and your main thread would also be working unnecessarily whenever it woke up.
Upvotes: 0