Skip to content

Commit b455d30

Browse files
committed
Reorder FAQ.
Add question on threads.
1 parent 773f0b6 commit b455d30

File tree

1 file changed

+49
-40
lines changed

1 file changed

+49
-40
lines changed

docs/howto/faq.rst

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,17 @@ websockets takes care of closing the connection when the handler exits.
131131
How do I run a HTTP server and WebSocket server on the same port?
132132
.................................................................
133133

134-
This isn't supported.
134+
You don't.
135+
136+
HTTP and WebSockets have widely different operational characteristics.
137+
Running them on the same server is a bad idea.
135138

136139
Providing a HTTP server is out of scope for websockets. It only aims at
137140
providing a WebSocket server.
138141

139142
There's limited support for returning HTTP responses with the
140143
:attr:`~legacy.server.WebSocketServerProtocol.process_request` hook.
144+
141145
If you need more, pick a HTTP server and run it separately.
142146

143147
Client side
@@ -287,6 +291,18 @@ There are several reasons why long-lived connections may be lost:
287291
If you're facing a reproducible issue, :ref:`enable debug logs <debugging>` to
288292
see when and how connections are closed.
289293

294+
How do I set a timeout on ``recv()``?
295+
.....................................
296+
297+
Use :func:`~asyncio.wait_for`::
298+
299+
await asyncio.wait_for(websocket.recv(), timeout=10)
300+
301+
This technique works for most APIs, except for asynchronous context managers.
302+
See `issue 574`_.
303+
304+
.. _issue 574: https://github.com/aaugustin/websockets/issues/574
305+
290306
How can I pass additional arguments to a custom protocol subclass?
291307
..................................................................
292308

@@ -307,31 +323,19 @@ You can bind additional arguments to the protocol factory with
307323

308324
This example was for a server. The same pattern applies on a client.
309325

310-
Why do I get the error: ``module 'websockets' has no attribute '...'``?
311-
.......................................................................
312-
313-
Often, this is because you created a script called ``websockets.py`` in your
314-
current working directory. Then ``import websockets`` imports this module
315-
instead of the websockets library.
316-
317-
Are there ``onopen``, ``onmessage``, ``onerror``, and ``onclose`` callbacks?
318-
............................................................................
319-
320-
No, there aren't.
326+
How do I keep idle connections open?
327+
....................................
321328

322-
websockets provides high-level, coroutine-based APIs. Compared to callbacks,
323-
coroutines make it easier to manage control flow in concurrent code.
329+
websockets sends pings at 20 seconds intervals to keep the connection open.
324330

325-
If you prefer callback-based APIs, you should use another library.
331+
In closes the connection if it doesn't get a pong within 20 seconds.
326332

327-
Can I use websockets synchronously, without ``async`` / ``await``?
328-
..................................................................
333+
You can adjust this behavior with ``ping_interval`` and ``ping_timeout``.
329334

330-
You can convert every asynchronous call to a synchronous call by wrapping it
331-
in ``asyncio.get_event_loop().run_until_complete(...)``. Unfortunately, this
332-
is deprecated as of Python 3.10.
335+
How do I respond to pings?
336+
..........................
333337

334-
If this turns out to be impractical, you should use another library.
338+
websockets takes care of responding to pings with pongs.
335339

336340
Miscellaneous
337341
-------------
@@ -344,31 +348,24 @@ websockets doesn't have built-in publish / subscribe for these use cases.
344348
Depending on the scale of your service, a simple in-memory implementation may
345349
do the job or you may need an external publish / subscribe component.
346350

347-
How do I set a timeout on ``recv()``?
348-
.....................................
349-
350-
Use :func:`~asyncio.wait_for`::
351-
352-
await asyncio.wait_for(websocket.recv(), timeout=10)
353-
354-
This technique works for most APIs, except for asynchronous context managers.
355-
See `issue 574`_.
356-
357-
.. _issue 574: https://github.com/aaugustin/websockets/issues/574
351+
Can I use websockets synchronously, without ``async`` / ``await``?
352+
..................................................................
358353

359-
How do I keep idle connections open?
360-
....................................
354+
You can convert every asynchronous call to a synchronous call by wrapping it
355+
in ``asyncio.get_event_loop().run_until_complete(...)``. Unfortunately, this
356+
is deprecated as of Python 3.10.
361357

362-
websockets sends pings at 20 seconds intervals to keep the connection open.
358+
If this turns out to be impractical, you should use another library.
363359

364-
In closes the connection if it doesn't get a pong within 20 seconds.
360+
Are there ``onopen``, ``onmessage``, ``onerror``, and ``onclose`` callbacks?
361+
............................................................................
365362

366-
You can adjust this behavior with ``ping_interval`` and ``ping_timeout``.
363+
No, there aren't.
367364

368-
How do I respond to pings?
369-
..........................
365+
websockets provides high-level, coroutine-based APIs. Compared to callbacks,
366+
coroutines make it easier to manage control flow in concurrent code.
370367

371-
websockets takes care of responding to pings with pongs.
368+
If you prefer callback-based APIs, you should use another library.
372369

373370
Is there a Python 2 version?
374371
............................
@@ -379,4 +376,16 @@ Python 2 reached end of life on January 1st, 2020.
379376

380377
Before that date, websockets required asyncio and therefore Python 3.
381378

379+
Why do I get the error: ``module 'websockets' has no attribute '...'``?
380+
.......................................................................
381+
382+
Often, this is because you created a script called ``websockets.py`` in your
383+
current working directory. Then ``import websockets`` imports this module
384+
instead of the websockets library.
385+
386+
I'm having problems with threads
387+
................................
388+
389+
You shouldn't use threads. Use tasks instead.
382390

391+
:func:`~asyncio.AbstractEventLoop.call_soon_threadsafe` may help.

0 commit comments

Comments
 (0)