Troubles while using test client and emitting from background thread. #2039
-
Greetings, I'm encountering a perplexing issue with Flask-SocketIO when attempting to emit from a background thread, specifically in a Dockerized environment utilizing Flask's factory pattern. The core of my project is orchestrated through a run.py file, which calls the create_app function to initialize my Flask application. Initially, all functionalities, including Flask-SocketIO's test client, operated flawlessly. However, the test client ceased to receive emits once I began emitting from a background thread. To better understand the problem, I've created a simplified version of my project structured as follows:
Test Code Overview Here's a snapshot of the test code employed:
The interesting part: observations and setupsInitial SetupThe initial configuration was as follows, situated within the /app directory:
--------------app.py---------------
When executed via Docker compose and unit tests ( Look at the output from the application and the test client:
test client:
Modified SetupI then refactored my project to align more closely with my main project's structure. This involved:
---------run.py------------
And modified the
After these adjustments, the test client failed to receive any emits, despite the application logs indicating successful emission. Look at the output from the application and the test client:
test client:
Seeking InsightsI'm puzzled by this discrepancy between the two setups. The only significant change was refactoring the code structure without altering the core logic. For those interested, my repository contains both setups on the "working-version" branch, allowing for direct comparison: The second setup can be found at: Any insights or suggestions would be greatly appreciated. Thank you for your time and assistance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You are using the Flask-SocketIO test client, which is not a real client, it runs directly in the server's context. The case that you think is working is actually running a second server in your client's container, and your test client is actually connecting to that server. You can see the server emitting in the log of your working client above. In the non-working case your client is running but the server is not, so the client connects and does not receive anything. If you want to run a real client-server test then you can't use the Flask-SocketIO test client, in that case you need to use a real Socket.IO client that connects over the network. |
Beta Was this translation helpful? Give feedback.
You are using the Flask-SocketIO test client, which is not a real client, it runs directly in the server's context. The case that you think is working is actually running a second server in your client's container, and your test client is actually connecting to that server. You can see the server emitting in the log of your working client above. In the non-working case your client is running but the server is not, so the client connects and does not receive anything.
If you want to run a real client-server test then you can't use the Flask-SocketIO test client, in that case you need to use a real Socket.IO client that connects over the network.