Its my first time using websockets with fastAPI and requests, Currently, I'm trying to display dots that change every 0.05 seconds based on API data but i just cant get the frequency to drop below 2 seconds. Its so slow and and I'm not sure what to do.
I've tried chunking and compression and neither of these methods work, is anyone able to suggest other ideas? I can share my code if you need specifics
#๐ Its my first time using websockets and I need help making them faster
24 messages ยท Page 1 of 1 (latest)
@digital inlet
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
You need to send a message every 50ms?
yes, is this not feasible?
I've never pushed websockets that far, but that seems incredibly frequent.
yes, i know it is very frequent, perhaps it is not the correct way to go about it. If you have any suggestions to solutions on alternate methods to solve this problem that would be greatly appreciated
are you saying that you are using the requests library in the same code as fastapi?
if that is so, you might have blocking code waiting for the requests functions/methods to return and you would probably be better off using an async library such as httpx or aiohttp to make the requests from the fastapi code
what does zlib do?
i'll try this now
thanks! good solution to my problem, it works pretty well, it went from ~2 seconds to ~ 0.4 seconds
going to try improve further using compression now
Can you paste the code?
Probably compression will slow it down
Could you try SSE instead of websockets?
glad it was of some help
when doing async programming it's important that all i/o (such as network calls, database calls and file io) is done async
With SSE you could use hypercorn with quic support
Also make sure you're pooling your http request connections using FastAPI lifespan_context
i'm sorry i don't know what SSE is, i'm a rookie at anything network based atm
SSE is server sent events
what are the benifits compared to websockets?
They tunnel inside http2 and quic
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.