python - Threads vs. Async - Stack Overflow
https://krondo.com/in-which-we-begin-at-the-beginning/
与threading相比, async的优势表现为3点:
- It is very difficult to write code that is thread safe. With asyncronous code, you know exactly where the code will shift from one task to the next and race conditions are therefore much harder to come by.
- Threads consume a fair amount of data since each thread needs to have its own stack. With async code, all the code shares the same stack and the stack is kept small due to continuously unwinding the stack between tasks.
- Threads are OS structures and are therefore more memory for the platform to support. There is no such problem with asynchronous tasks.
大致意思:
1. 代码很难保证线程时安全的,而async 代码,可以很清楚的看到代码之间的调换/执行过程
2.每个线程有自己单独的栈,而async代码之间共享栈
3. 线程比较耗内存
本文探讨了async代码相较于threading的三个优势:代码线程安全性增强,内存占用更少,以及对平台资源需求较低。通过实例说明了如何避免竞态条件和共享栈的问题,以及async如何简化内存管理。
296

被折叠的 条评论
为什么被折叠?



