nameko 和 peewee 混合使用会影响 TLS 复用数据库连接吗?

2年前 (2022) 程序员胖胖胖虎阿
240 0 0

会得 peewee 用的是 threading.local 来实现 TLS

class _ConnectionLocal(_ConnectionState, threading.local): pass

nameko 并发模型用的是 eventlet 的 pool。处理每个任务的时候,都会创建一个 greenthread,然后提交到 pool 中去跑。

eventlet 的 pool 和 python 的 thread pool 不一样。

python 的 thread pool 是解决 thread 频繁创建和销毁占用资源的问题。所以 python 的 thread pool 是复用线程的。

但是协程的创建和销毁是非常廉价的,所以,没有复用 greenthread 的需求。eventlet 的 pool 不会复用 greenthread。

eventlet 的用法是:创建一个 greenthread,然后提交到 pool 中,跑完了 greenthread 也就被销毁了!那这个 pool 还有什么用呢?我觉得:可以借助 pool 的 size 方便的控制并发数!

相关文章

暂无评论

暂无评论...