| 1 |
Using asyncio |
| 2 |
============= |
| 3 |
|
| 4 |
.. currentmodule:: websockets.asyncio.connection |
| 5 |
|
| 6 |
.. admonition:: This FAQ is written for the new :mod:`asyncio` implementation. |
| 7 |
:class: tip |
| 8 |
|
| 9 |
Answers are also valid for the legacy :mod:`asyncio` implementation. |
| 10 |
|
| 11 |
How do I run two coroutines in parallel? |
| 12 |
---------------------------------------- |
| 13 |
|
| 14 |
You must start two tasks, which the event loop will run concurrently. You can |
| 15 |
achieve this with :func:`asyncio.gather` or :func:`asyncio.create_task`. |
| 16 |
|
| 17 |
Keep track of the tasks and make sure that they terminate or that you cancel |
| 18 |
them when the connection terminates. |
| 19 |
|
| 20 |
Why does my program never receive any messages? |
| 21 |
----------------------------------------------- |
| 22 |
|
| 23 |
Your program runs a coroutine that never yields control to the event loop. The |
| 24 |
coroutine that receives messages never gets a chance to run. |
| 25 |
|
| 26 |
Putting an ``await`` statement in a ``for`` or a ``while`` loop isn't enough |
| 27 |
to yield control. Awaiting a coroutine may yield control, but there's no |
| 28 |
guarantee that it will. |
| 29 |
|
| 30 |
For example, :meth:`~Connection.send` only yields control when send buffers are |
| 31 |
full, which never happens in most practical cases. |
| 32 |
|
| 33 |
If you run a loop that contains only synchronous operations and a |
| 34 |
:meth:`~Connection.send` call, you must yield control explicitly with |
| 35 |
:func:`asyncio.sleep`:: |
| 36 |
|
| 37 |
async def producer(websocket): |
| 38 |
message = generate_next_message() |
| 39 |
await websocket.send(message) |
| 40 |
await asyncio.sleep(0) # yield control to the event loop |
| 41 |
|
| 42 |
:func:`asyncio.sleep` always suspends the current task, allowing other tasks |
| 43 |
to run. This behavior is documented precisely because it isn't expected from |
| 44 |
every coroutine. |
| 45 |
|
| 46 |
See `issue 867`_. |
| 47 |
|
| 48 |
.. _issue 867: https://github.com/python-websockets/websockets/issues/867 |
| 49 |
|
| 50 |
Why am I having problems with threads? |
| 51 |
-------------------------------------- |
| 52 |
|
| 53 |
If you choose websockets' :mod:`asyncio` implementation, then you shouldn't use |
| 54 |
threads. Indeed, choosing :mod:`asyncio` to handle concurrency is mutually |
| 55 |
exclusive with :mod:`threading`. |
| 56 |
|
| 57 |
If you believe that you need to run websockets in a thread and some logic in |
| 58 |
another thread, you should run that logic in a :class:`~asyncio.Task` instead. |
| 59 |
|
| 60 |
If it has to run in another thread because it would block the event loop, |
| 61 |
:func:`~asyncio.to_thread` or :meth:`~asyncio.loop.run_in_executor` is the way |
| 62 |
to go. |
| 63 |
|
| 64 |
Please review the advice about :ref:`asyncio-multithreading` in the Python |
| 65 |
documentation. |
| 66 |
|
| 67 |
Why does my simple program misbehave mysteriously? |
| 68 |
-------------------------------------------------- |
| 69 |
|
| 70 |
You are using :func:`time.sleep` instead of :func:`asyncio.sleep`, which |
| 71 |
blocks the event loop and prevents asyncio from operating normally. |
| 72 |
|
| 73 |
This may lead to messages getting send but not received, to connection timeouts, |
| 74 |
and to unexpected results of shotgun debugging e.g. adding an unnecessary call |
| 75 |
to a coroutine makes the program functional. |