5

In the python asyncio websockets library, the example calls run_forever(). Why is this required?

Shouldn't run_until_complete() block and run the websockets loop?

#!/usr/bin/env python

# WS server example

import asyncio
import websockets

async def hello(websocket, path):
    name = await websocket.recv()
    print(f"< {name}")

    greeting = f"Hello {name}!"

    await websocket.send(greeting)
    print(f"> {greeting}")

start_server = websockets.serve(hello, "localhost", 8765)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
# if you comment out the above line, this doesn't work, i.e., the server
# doesn't actually block waiting for data...

If I comment out run_forever(), the program ends immediately.

start_server is an awaitable returned by the library. Why isn't run_until_complete sufficient to cause it to block/await on hello()?

0

1 Answer 1

3

websockets.serve simply starts the server and exits immediately. (It still needs to be awaited because configuring the server can require network communication.) Because of that, you need to actually run the event loop.

Since the server is designed to run indefinitely, you cannot run the event loop in the usual way, by passing a coroutine to run_until_complete. As the server has already started, there is no coroutine to run, you just need to let the event loop run and do its job. This is where run_forever comes in useful - it tells the event loop to run (executing the tasks previously scheduled, such as those belonging to the server) indefinitely, or until told to stop by a call to loop.stop.

In Python 3.7 and later one should use asyncio.run to run asyncio code, which will create a new event loop, so the above trick won't work. A good way to accomplish the above in modern asyncio code would be to use the serve_forever method (untested):

async def my_server():
    ws_server = await websockets.serve(hello, "localhost", 8765)
    await ws_server.server.serve_forever()

asyncio.run(my_server())
2
  • Thank you - I realise now that Protocols need the event loop to be around to do anything useful. It is confusing: Protocols are like ghost Tasks, if you don't keep the loop alive they quietly die, but they don't appear in asyncio.all_tasks(). Wish there was a method asyncio.show_why_i_shouldnt_close_this_loop() returns set of working protocols. The autobahn people also use the pattern transport, protocol = loop.run_until_complete(coroutine) # then... loop.run_forever() Nov 8, 2019 at 6:58
  • @AnthonyAlba The event loop has to be around for anything asyncio-related to work, not just protocols and tasks. Protocols are not ghost tasks, they are a different high-level abstraction that uses callbacks to do its magic. The callbacks are installed using the basic event loop methods like add_reader, add_writer, call_later, call_soon, etc., and it is these that need the event loop actually running to keep the wheels turning. Nov 8, 2019 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.