This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Ilya.Kulakov
Recipients Ilya.Kulakov, lukasz.langa, methane, ncoghlan
Date 2021-01-16.21:46:18
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1610833579.12.0.993134178442.issue42943@roundup.psfhosted.org>
In-reply-to
Content
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()))`.
History
Date User Action Args
2021-01-16 21:46:19Ilya.Kulakovsetrecipients: + Ilya.Kulakov, ncoghlan, methane, lukasz.langa
2021-01-16 21:46:19Ilya.Kulakovsetmessageid: <1610833579.12.0.993134178442.issue42943@roundup.psfhosted.org>
2021-01-16 21:46:19Ilya.Kulakovlinkissue42943 messages
2021-01-16 21:46:18Ilya.Kulakovcreate