In my project I use a Worker to do some event related file processing and avoid blocking the handling of other events. What I need is to be able to put the event (that requires the file processing) in stand by (no further handling by other Components) while the Worker finishes and, then, resume the handling sequence.
I can't issue a new event when the Worker finishes because that may cause other Components to re-process the "same" event more than once.
Is there any way to delay (i.e. suspend and resume) the event propagation to accomplish what I need? Is there a better way to solve my use case?
Additional note: I can't avoid the blocking behavior because I need to use some external (blocking) library calls.
EDIT: Source code example:
from time import sleep
from circuits import Component, Debugger, handler, Event, Worker, task
class my_event(Event):
pass
def heavy_task():
print "heavy task"
sleep(3)
class NextHandler(Component):
@handler("my_event", priority=10)
def my_event(self, event):
print "Handler 2"
class Handler(Component):
_worker = Worker()
@handler("my_event", priority=20)
def my_event(self, event):
self.fire(task(heavy_task), self._worker)
print "Handler 1"
# how do I delay "event" until "heavy_task" is completed?
class App(Component):
h1 = Handler()
h2 = NextHandler()
def started(self, component):
print "Running"
self.fire(my_event())
if __name__ == '__main__':
(App() + Debugger()).run()
In this case I want to delay "Handler" event so "NextHandler" don't receive it until "heavy_task" has finished working.