Async event execution

32 views
Skip to first unread message

Hugh Pyle

unread,
Feb 4, 2016, 4:36:48 PM2/4/16
to circuits
Hi folks,

I've been using the "task"/"worker" a bit, and found that it's simple enough to put together, but the moving parts confuse me each time.

So here's a decorator class that allows you to decorate a handler method and "just do it".

This does assume that you have a worker attached to the application, and to a channel "worker" (or a custom name passed to the decorator).  It's not obvious to me how to verify at decoration-time or execution-time that the task will actually be picked up by a worker.  Any suggestions how to do that?


Hugh

--
from functools import wraps

class async(object):
    """Decorator for an event handler, pushes this event execution into a worker pool"""
    def __init__(self, *args, **kwargs):
        self.pool = kwargs.get("pool", "worker")
        if len(args) > 0:
            raise Exception("Usage: @async() or @async(pool=<worker_channel_name>)")

    def __call__(self, func):
        """Called at decoration time, with function"""
        @wraps(func)
        def decorated(itself, event, *args, **kwargs):
            evt = task(func, itself, event, *args, **kwargs)
            ret = yield itself.call(evt, self.pool)
            result = ret.value
            if isinstance(result, Exception):
                raise result
            yield result
        return decorated


# example usage
@async()
@handler()
def _method(...)
    # your event handler is now running on a thread or process from the worker


prol...@shortcircuit.net.au

unread,
Feb 4, 2016, 4:39:27 PM2/4/16
to circuit...@googlegroups.com
Hi Hugh,

Would you mind contributing this to circuits as a pull-request to some appropriate part of the circuits core library? Perhaps in `circuits.core.async` or something? That would be awesome :) And nice work btw!

https://github.com/circuits/circuits

cheers
James
--
You received this message because you are subscribed to the Google Groups "circuits" group.
To unsubscribe from this group and stop receiving emails from it, send an email to circuits-user...@googlegroups.com.
To post to this group, send email to circuit...@googlegroups.com.
Visit this group at https://groups.google.com/group/circuits-users.
For more options, visit https://groups.google.com/d/optout.

 

James Mills

unread,
Feb 5, 2016, 2:22:09 AM2/5/16
to circuit...@googlegroups.com

On Thu, Feb 4, 2016 at 11:27 AM, Hugh Pyle <hp...@cabezal.com> wrote:
This does assume that you have a worker attached to the application, and to a channel "worker" (or a custom name passed to the decorator).  It's not obvious to me how to verify at decoration-time or execution-time that the task will actually be picked up by a worker.  Any suggestions how to do that?

You *could* search for a Worker instance in the component graph.


cheers
Reply all
Reply to author
Forward
0 new messages