Message385151
The wrapper created by singledispatchmethod does not (trivially) expose registry of all known overloads.
Consider the following example:
@singledispatchmethod
def on_message(message):
raise NotImplementedError
@on_message.register
def _(message: MessageA): ...
@on_message.register
def _(message: MessageB): ...
loop = ...
condition = ...
messages = []
def receive_message_on_thread(message):
if ...: # only signal to asyncio if message can be handled
messages.append(message)
loop.call_soon_threadsafe(condition.notify_all)
else:
...
async def process_messages_on_asyncio():
while True:
await condition.wait_for(lambda: len(messages) > 0)
while len(messages):
m = messages.pop(0)
...
Here messages are delivered outside of asyncio in a separate thread and thus they need to be forwarded into asyncio for further processing in the app. If the flow of messages is high it is desirable to filter inside the thread handler before signaling asyncio.
There are multiple approaches to describe which messages can be handled by the asyncio processor, but singledispatchmethod is the most elegant as it allows to keep everything in one place without if/else or code duplication.
Having a registry (trivially) exposed would allow to write the condition as `isinstance(message, tuple(on_message.registry.keys()))`. |
|
Date |
User |
Action |
Args |
2021-01-16 21:46:19 | Ilya.Kulakov | set | recipients:
+ Ilya.Kulakov, ncoghlan, methane, lukasz.langa |
2021-01-16 21:46:19 | Ilya.Kulakov | set | messageid: <1610833579.12.0.993134178442.issue42943@roundup.psfhosted.org> |
2021-01-16 21:46:19 | Ilya.Kulakov | link | issue42943 messages |
2021-01-16 21:46:18 | Ilya.Kulakov | create | |
|