zlacker

[return to "The bane of my existence: Supporting both async and sync code in Rust"]
1. doakes+nq[view] [source] 2024-01-20 00:52:30
>>lukast+(OP)
What's the advantage of async if you immediately call await?
◧◩
2. sodali+sr[view] [source] 2024-01-20 01:03:44
>>doakes+nq
If you're I/O bound, it allows for easy and efficient utilization of resources by enabling other tasks to run while waiting for I/O.

I'll give you a real world example. I wrote some code that listened to a websockets URL from thousands of Reddit posts - specifically, the one that sends new messages on new comments - so I could see a stream of Reddit comments for any given sub.

Implemented it using Tungstenite (synchronous) and it created thousands of threads to listen, and used enormous chunks of memory (several GB) for the stack space + memory reading for every single WS stream.

Implemented it using Tokio_tungstenite, the async alternative, and it used a handful of MB of memory and barely any CPU to listen to thousands of WS servers.

[go to top]