milibopp / obsub

Small python module that implements the observer pattern via a decorator.
Other
12 stars 3 forks source link

Add support for asynchronous event handling #11

Closed milibopp closed 10 years ago

milibopp commented 11 years ago

So I have been thinking about asynchronicity lately. And I was wondering whether it would be a good idea to have obsub support event handling in an asynchronous way. Now this is probably a big step but might be worth considering I think.

There are a couple of tools providing the functionality required. gevent, eventlet and concurrence are probably interesting ones. They all use greenlet, which is basically the green thread functionality of Stackless implemented for CPython.

On that note, it might be interesting to consider running on Stackless as well, which would make it more natural to run asynchronously as well. Especially in the context of writing game logic, which was the original intention for this package, this might make a lot of sense.

Please tell me what you think about this. When I have the time, I might implement some prototype.

coldfix commented 11 years ago

I think this should be implemented in an independent module. Like this:

@async_method
@event
def on_foo(self, arg):
    pass

This keeps the event module lightweight and the concerns separated. Of course the async_method decorator has to be developed with care in order to interact well with other decorators that may even return descriptors (as is the case with obsub.event).

Moredread commented 11 years ago

how does synchronization work in such a szenario?

milibopp commented 10 years ago

@Moredread If you use greenlets, only one of those is running at any given time. And they only halt their execution (and let another greenlet run), if they reach an explicit statement that tells them to. For convenience a lot of usually blocking I/O operations like file access, waiting for a network response, etc. are implemented in such a way that a greenlet handling them lets other greenlets run, while it is waiting for these operations to finish. So this is not real parallelism.

milibopp commented 10 years ago

@coldfix I do consider this a separate concern obviously, though I think it is somewhat related to event handling. So maybe making a separate package is a sensible thing, but I have to think about the kind of design patterns I would actually like to use this for. I'm thinking about the actor model here mainly, where an asynchronous event handler would be the method of communication between different actors. For the use case obsub comes from (game development) this could be useful, I think.