crossbario / crossbario/autobahn-python

[Question] How to run examples idiomatically with asyncio.run()?

Open
#1,265 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.5k
Forks
768
PR merge metrics
No merged PRs in 30d

Description

Python documentation encourages users to use asyncio.run to launch the "main" coroutine. ApplicationRunner does not expose how to integrate itself into that pattern, as the run() method encapsulates the loop.run_forever().

One use case is to add other non-WAMP coroutines to the event loop:

# Desired use case: integrate non-autobahn tasks into the event loop
# 
async def main():
    coro = runner.hypothetical_asyncio_friendly_method(...)
    # then
    t1 = asyncio.create_task()
    t2 = asyncio.create_task()
    tN = asyncio.create_task()
    await asyncio.gather(t1, t2, t3..., coro)

if __name__ == "__main__":
    asyncio.run(main())

Specifically, how do you convert the examples to idiomatic asyncio.run? Attempting to do this with
examples/asyncio/wamp/rpc/complex/frontend.py.

  1. First attempt
    runner = ApplicationRunner(url, realm)
    coro = runner.run(Component, start_loop=False)

    asyncio.run(coro) # blows up

  ## OK so this coro is not suitable to be a asyncio main() ...
  ## ...it is the return value of loop.create_connection()
  ## ...in any case asyncio.run wants to create a new event loop

Results in:

RuntimeError: Task <Task pending coro=<BaseEventLoop.create_connection() running at /usr/lib64/python3.7/asyncio/base_events.py:941>
cb=[_run_until_complete_cb() at /usr/lib64/python3.7/asyncio/base_events.py:153]>
got Future <Future pending cb=[BaseSelectorEventLoop._sock_connect_done(9)()]>
attached to a different loop

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with examples/asyncio/wamp/rpc/complex/frontend.py and inspect ApplicationRunner.run, especially its loop handling and start_loop=False path. Determine an idiomatic asyncio.run integration that can coexist with additional non-WAMP coroutines, then update the example or exposed API so the behavior is demonstrated without cross-loop errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.