Comments (5)
Interestingly, rewriting the above code as below does work as expected. So it only seems to be an issue when you run a websocket inside a class that is a subclass of Thread
, rather than in a Thread
itself, necessarily:
from websockets.sync import client
from threading import Thread
def ws():
ws = client.connect("wss://echo.websocket.org")
for msg in ws:
print(msg)
t = Thread(target=ws)
t.daemon = True
t.start()
print("Thread started. Now trying to exit")
exit()
# This does exit cleanly as we expect.
from websockets.
Actually, with a few more tests this morning, I narrowed down the issue a bit more. Seems to be if the client
is passed into a Thread
that runs the read loop, then that prevents process exit, even if the Thread
is a daemon. So, taking the code above that exits successfully, if we simply move the instantiation of the client
and pass it into the Thread
then the process now cannot exit as long as the client
connection is open:
from websockets.sync import client
from threading import Thread
def ws(sock):
for msg in sock:
print(msg)
c = client.connect("wss://echo.websocket.org")
t = Thread(target=ws, args=(c,))
t.daemon = True
t.start()
print("Thread started. Now trying to exit")
exit()
# Never exits
This is a pain as it means that we cannot have a continuous read in a daemon thread, whilst keeping the reference to the client
so we can asyncronously send
. Unless, as I have done in my own code to get around this, we run an async client in a daemon thread (which the documentation states is not supported).
from websockets.
I was looking into this and the reason this is happening is because the client.connect()
call constructs the ClientConnection
object, which starts the recv_events_thread
. It does so without specifying the daemon
parameter to the thread creation: https://github.com/python-websockets/websockets/blob/main/src/websockets/sync/connection.py#L86
This means this thread inherits the daemon status from its parent. So if you want the WebSocket connection's "background" thread to truly be a daemon thread, it must be created from within a thread that is itself a daemon – and the main thread of the Python program is not.
This certainly would be helpful to have noted in the docs of the sync client.
from websockets.
Perhaps the background thread should be a daemon thread? If the main thread as well as any other thread managed by the user of the library is done, there's no reason to prevent the program from exiting.
we run an async client in a daemon thread (which the documentation states is not supported).
It works. I'm discouraging it because:
- there's little benefit to running an event loop just for one client connection;
- many users of websockets don't have a clear mental model of what an event loop is and, as a consequence, struggle to start it in the right thread and use
call_soon_threadsafe
properly.
from websockets.
@aaugustin I agree, thanks for making the change! Let us know when you cut a release.
from websockets.
Related Issues (20)
- Missing tests for TLS connections in sync implementation
- Documentation: Hello-world example using threading instead of asyncio HOT 1
- Duplicate ping/pong ? HOT 2
- Testing against a running websockets server HOT 1
- 0 bytes read on a total of 2 expected bytes HOT 2
- Secure websocket using example code unable to handle unsecured requests HOT 4
- No route to host----when access IPv6 address failed,no attempt was made to access an IPv4 address HOT 1
- How to close a client connection using the threading interface while stuck in ClientConnection.recv
- Can we get more attention in the threaded client interface? HOT 1
- Human-readable representation of a frame is too short HOT 2
- No module websockets.imports HOT 2
- Python 3.11 incompatible with websockets\legacy\protocol.py Error "got an unexpected keyword argument 'loop'" HOT 1
- Strange problem, client cannot receive messages from the server, after sending a certain amount of messages. HOT 2
- Cannot Send message to a single client.
- handling multiple websocket client connections HOT 4
- Enable to connect ! invalid handshake HOT 7
- recv() is slow HOT 5
- Client state is incorrect HOT 1
- Feature batch receive HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from websockets.