On Fri, Oct 23, 2015 at 7:16 AM, Yusuke Iwase <[email protected]> wrote:
> If you want to call self.WorkOn(ev.msg) asynchronously, how about using 
> ryu.lib.hub.spawn function?
> spawn (or spawn_after) invokes the new thread.
>
> e.g.)
> from ryu.lib import hub
> ...
>     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
>     def event_catcher(self, ev):
>         hub.spawn(self.WorkOn, ev.msg)

Along these same lines, we use a separate base class with an
overridden event loop to spawn all events async and keep track of the
threads so that the join in `stop` will wait for the threads to
finish:

class AsyncEventsRyuApp(app_manager.RyuApp):
    # NOTE(jkoelker) Override app_manager.RyuApp._event_loop to start
    #                handlers each in their own thread so they don't
    #                block each other
    def _event_loop(self):
        def event_done(gt, *args, **kwargs):
            while gt in self.threads:
                self.threads.remove(gt)

        while self.is_active or not self.events.empty():
            ev, state = self.events.get()

            if ev == self._event_stop:
                continue

            handlers = self.get_handlers(ev, state)
            for handler in handlers:
                t = hub.spawn(handler, ev)
                t.link(event_done)
                self.threads.append(t)

            hub.sleep(0)

We have yet to put anything into production that is based off this,
but we have some code in our staging environments and it seems to work
generally well.

Happy Hacking!

7-11

------------------------------------------------------------------------------
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to