Skip to content

Since 3.8.0 ApplicationCommunicator cannot be constructed in a synchronous context #501

Closed
@riconnon

Description

@riconnon

#367 changed the initialisation of the task in ApplicationCommunicator from using asyncio.ensure_future to using asyncio.create_task

While is is generally a positive change and use of asyncio.create_task is generally preferred over low-level primitives like asyncio.ensure_future this has the side effect that, since asyncio.create_task uses asyncio.get_running_loop under the hood where asyncio.ensure_future used asyncio.get_event_loop, an ApplicationCommunicator can no longer be constructed outside of a coroutine or callback

In my test suite I'm using unittest with synchronous test functions and I have code to explicitly manage event loops and configure them for use using asyncio.set_event_loop

I've been able to workaround this using code like the following:

def _task():
    return ApplicationCommunicator(*args, **kwargs)
task = loop.create_task(_task())
while not task.done():
    pass
return task.result()

I wonder if this code could, instead, be calling asyncio.get_event_loop and calling loop.create_task instead of asyncio.create_task so that it works correctly in a synchronous context with an assigned event loop.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions