zope.interface icon indicating copy to clipboard operation
zope.interface copied to clipboard

Feature. Support asyncio events

Open jordic opened this issue 3 years ago • 5 comments

We use zope.interface and the ZCA almost on every new project we start. The only peace we miss, it's being able to subscribe to async event handlers. We already do it, with a bit of inheritance but, does it make sense to provide a PR to being to support it on the default package?

jordic avatar Sep 07 '20 15:09 jordic

Can you please clarify what you mean by "subscribe to async event handlers"?

jamadden avatar Sep 07 '20 16:09 jamadden

For example: await notify(SomeEvent(obj)) and async def subscriber(event)

jordic avatar Sep 08 '20 03:09 jordic

notify isn't defined here, that's over in zope.event. Because it wouldn't be backwards compatible, and because it still supports versions of Python that don't have async def/await I would imagine the odds of making notify into an async def function are extremely small.

As for async def subscriber(…):

zope.interface is connected to zope.event via zope.component, which uses the subscribers() API:

def dispatcher(event):
    zope.component.getSiteManager().subscribers(event, None)
zope.event.subscribers.append(dispatcher)

The subscription functions are defined to have no return value for the notify case. Without expensive runtime checking (which I would also imagine is a non-starter because this is extremely performance sensitive code), how is this loop supposed to deal with them? https://github.com/zopefoundation/zope.interface/blob/255db9d3c0ea7a5016e3c87644c23fca6eda9a7d/src/zope/interface/adapter.py#L614-L626

jamadden avatar Sep 08 '20 10:09 jamadden

I'd say if you can create a PR that adds support for Python async in a backwards-compatible way, go for it!

mgedmin avatar Sep 09 '20 06:09 mgedmin

What we are using, it's just and inherited adapter registry that supports async subscrbers. We use, zope.interface, standalone, without relaying on zope.event neither zope.component, because we don't support persistent registries neither context based registres. Addding something like this, will allow us to clean some code, that we are using everywhere.. (The AsyncAdapter registry)

jordic avatar Sep 10 '20 08:09 jordic