zope.interface
zope.interface copied to clipboard
Feature. Support asyncio events
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?
Can you please clarify what you mean by "subscribe to async event handlers"?
For example: await notify(SomeEvent(obj))
and async def subscriber(event)
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
I'd say if you can create a PR that adds support for Python async in a backwards-compatible way, go for it!
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)