Closed louking closed 10 months ago
Another instance, another log.
reader (missed pos = 12? also some log messages appear to be missing, e.g., data received, msg processed for pos = 13,14
2023-08-19 17:00:06,043 tm-csv-connector DEBUG: data received: b'\x19XC 00:00:00.00 \r\n'
2023-08-19 17:00:06,044 tm-csv-connector DEBUG: msg processed: b'\x19XC 00:00:00.00 '
2023-08-19 17:00:16,685 tm-csv-connector DEBUG: data received: b'\x00EVENT 072\r\n\x1711 001 0001 00:00:10.63 \r\n'
2023-08-19 17:00:16,685 tm-csv-connector DEBUG: msg processed: b'\x00EVENT 072'
2023-08-19 17:00:16,686 tm-csv-connector DEBUG: msg processed: b'\x1711 001 0001 00:00:10.63 '
2023-08-19 17:00:16,728 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 1, "time": "0:00:10.63"}
2023-08-19 17:00:16,763 tm-csv-connector DEBUG: data received: b'\x1711 0'
2023-08-19 17:00:16,764 tm-csv-connector DEBUG: residual: b'\x1711 0'
2023-08-19 17:00:17,066 tm-csv-connector DEBUG: data received: b'02 0002 00:00:10.77 \r\n\x1711 003 0003 00:00:10.93 \r\n\x17'
2023-08-19 17:00:17,067 tm-csv-connector DEBUG: residual: b'\x17'
2023-08-19 17:00:17,067 tm-csv-connector DEBUG: msg processed: b'\x1711 002 0002 00:00:10.77 '
2023-08-19 17:00:17,068 tm-csv-connector DEBUG: msg processed: b'\x1711 003 0003 00:00:10.93 '
2023-08-19 17:00:17,375 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 2, "time": "0:00:10.77"}
2023-08-19 17:00:17,393 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 3, "time": "0:00:10.93"}
2023-08-19 17:00:17,411 tm-csv-connector DEBUG: data received: b'11 004 0004 00:00:11.09 \r\n\x1711 005 0005 00:00:11.24 \r\n\x1711 006 0006 00:00:11.40 \r\n'
2023-08-19 17:00:17,411 tm-csv-connector DEBUG: msg processed: b'\x1711 004 0004 00:00:11.09 '
2023-08-19 17:00:17,412 tm-csv-connector DEBUG: msg processed: b'\x1711 005 0005 00:00:11.24 '
2023-08-19 17:00:17,412 tm-csv-connector DEBUG: msg processed: b'\x1711 006 0006 00:00:11.40 '
2023-08-19 17:00:17,722 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 4, "time": "0:00:11.09"}
2023-08-19 17:00:17,736 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 5, "time": "0:00:11.24"}
2023-08-19 17:00:17,753 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 6, "time": "0:00:11.40"}
2023-08-19 17:00:17,773 tm-csv-connector DEBUG: data received: b'\x1711 007 0007 00:00:11.57 \r\n\x1711 008 0008 00:00:11.74 \r\n'
2023-08-19 17:00:17,773 tm-csv-connector DEBUG: msg processed: b'\x1711 007 0007 00:00:11.57 '
2023-08-19 17:00:17,774 tm-csv-connector DEBUG: msg processed: b'\x1711 008 0008 00:00:11.74 '
2023-08-19 17:00:18,068 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 7, "time": "0:00:11.57"}
2023-08-19 17:00:18,083 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 8, "time": "0:00:11.74"}
2023-08-19 17:00:18,089 tm-csv-connector DEBUG: data received: b'\x1711 009 0009 00:00:11.90 \r\n\x1711 010 0010 00:00:12.06'
2023-08-19 17:00:18,090 tm-csv-connector DEBUG: residual: b'\x1711 010 0010 00:00:12.06'
2023-08-19 17:00:18,090 tm-csv-connector DEBUG: msg processed: b'\x1711 009 0009 00:00:11.90 '
2023-08-19 17:00:18,399 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 9, "time": "0:00:11.90"}
2023-08-19 17:00:18,405 tm-csv-connector DEBUG: data received: b' \r\n\x1711 011 0011 00:00:12.23 \r\n\x1711 012 0012 00:00:12.4'
2023-08-19 17:00:18,406 tm-csv-connector DEBUG: residual: b'\x1711 012 0012 00:00:12.4'
2023-08-19 17:00:18,406 tm-csv-connector DEBUG: msg processed: b'\x1711 010 0010 00:00:12.06 '
2023-08-19 17:00:18,406 tm-csv-connector DEBUG: msg processed: b'\x1711 011 0011 00:00:12.23 '
2023-08-19 17:00:18,728 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 10, "time": "0:00:12.06"}
2023-08-19 17:00:18,742 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 11, "time": "0:00:12.23"}
2023-08-19 17:00:19,088 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 13, "time": "0:00:12.57"}
2023-08-19 17:00:19,105 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 14, "time": "0:00:12.73"}
2023-08-19 17:00:19,112 tm-csv-connector DEBUG: data received: b'\x1711 015 0015 00:00:12.92 \r\n'
2023-08-19 17:00:19,114 tm-csv-connector DEBUG: msg processed: b'\x1711 015 0015 00:00:12.92 '
2023-08-19 17:00:19,435 tm-csv-connector DEBUG: sending: {"opcode": "primary", "pos": 15, "time": "0:00:12.92"}
web server sees all 15
2023-08-19 17:00:16 192.168.0.1 - - [19/Aug/2023:21:00:16 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:17 192.168.0.1 - - [19/Aug/2023:21:00:17 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:17 192.168.0.1 - - [19/Aug/2023:21:00:17 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:17 192.168.0.1 - - [19/Aug/2023:21:00:17 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:17 192.168.0.1 - - [19/Aug/2023:21:00:17 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:17 192.168.0.1 - - [19/Aug/2023:21:00:17 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:18 192.168.0.1 - - [19/Aug/2023:21:00:18 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:18 192.168.0.1 - - [19/Aug/2023:21:00:18 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:18 192.168.0.1 - - [19/Aug/2023:21:00:18 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:18 192.168.0.1 - - [19/Aug/2023:21:00:18 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:18 192.168.0.1 - - [19/Aug/2023:21:00:18 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:19 192.168.0.1 - - [19/Aug/2023:21:00:19 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:19 192.168.0.1 - - [19/Aug/2023:21:00:19 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:19 192.168.0.1 - - [19/Aug/2023:21:00:19 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
2023-08-19 17:00:19 192.168.0.1 - - [19/Aug/2023:21:00:19 +0000] "GET /tm_reader HTTP/1.1" 101 4 "-" "Python/3.10 websockets/11.0.3"
but app is missing some
2023-08-19 17:00:16 [2023-08-19 21:00:16,730] DEBUG in websockets_server: received data {"opcode": "primary", "pos": 1, "time": "0:00:10.63"}
2023-08-19 17:00:17 [2023-08-19 21:00:17,377] DEBUG in websockets_server: received data {"opcode": "primary", "pos": 2, "time": "0:00:10.77"}
2023-08-19 17:00:17 [2023-08-19 21:00:17,395] DEBUG in websockets_server: received data {"opcode": "primary", "pos": 3, "time": "0:00:10.93"}
2023-08-19 17:00:17 [2023-08-19 21:00:17,739] DEBUG in websockets_server: received data {"opcode": "primary", "pos": 5, "time": "0:00:11.24"}
2023-08-19 17:00:18 [2023-08-19 21:00:18,085] DEBUG in websockets_server: received data {"opcode": "primary", "pos": 8, "time": "0:00:11.74"}
2023-08-19 17:00:18 [2023-08-19 21:00:18,730] DEBUG in websockets_server: received data {"opcode": "primary", "pos": 10, "time": "0:00:12.06"}
2023-08-19 17:00:19 [2023-08-19 21:00:19,090] DEBUG in websockets_server: received data {"opcode": "primary", "pos": 13, "time": "0:00:12.57"}
2023-08-19 17:00:19 [2023-08-19 21:00:19,438] DEBUG in websockets_server: received data {"opcode": "primary", "pos": 15, "time": "0:00:12.92"}
I don't really know how to interpret these logs, since there is no code that demonstrates how each side works with the WebSocket. But in any case, there is something that is very strange in which it appears that each message is sent on a separate WebSocket connection. When you say that the web server sees 15 messages and you demonstrate by showing 15 WebSocket connections that is wrong, the correct solution would be a single WebSocket connection receiving all the messages.
All of the code is at https://github.com/louking/tm-csv-connector/tree/e114036e6c024eaae4ad1d26f68788655b45b11b
The messages are being sent from https://github.com/louking/tm-csv-connector/blob/e114036e6c024eaae4ad1d26f68788655b45b11b/tm-reader-client/app.py#L134-L158. Originally the loop had
async with connect(backenduri) as websocket:
while len(queued_msgs) > 0:
which would be more efficient, but this was an attempt to fix the lost messages. Following your suggestion of "the correct solution" I guess I could move the connection outside the while True
loop. Having said that, while inefficient, I'm not sure why the way it's currently coded wouldn't work.
The receiver is at https://github.com/louking/tm-csv-connector/blob/e114036e6c024eaae4ad1d26f68788655b45b11b/app/src/tm_csv_connector/websockets_server.py#L28-L44
Why are you sending like this?
while len(queued_msgs) > 0:
async with connect(backenduri) as websocket:
msg = queued_msgs.pop(0)
await send_to_backend(websocket, msg)
This is creating a new connection for each message, and it then closes the connection, possibly before giving time for the message to be sent (which I believe happens in a background task).
As I said before, I continue to believe this is a bug in your client application, not an issue with Flask-Sock or Flask-SocketIO on the server.
Ok. I thought the async with connect()
would await the connection to be set up before allowing the message to be sent, and that await send_to_backend()
would not allow the with
block to exit.
Good input, though, and I'll investigate moving the connect outside the while True
loop.
Yup, that was it. Thanks for your patience and advice @miguelgrinberg.
Fixed with https://github.com/louking/tm-csv-connector/commit/c3da31aed681ee892f4557b59a6f06468438d026
I meant to note I also needed to use eventlet, else the flask app would get a worker timeout and restart. Possibly this could have been handled with gunicorn threads as well
see
reader
backend