python - SimpleConnectionPool vs ThreadedConnectionPool:线程安全意味着什么?
问题描述
我试图找出psycopg2 连接池SimpleConnectionPool
之间的区别。ThreadedConnectionPool
文档说:
SimpleConnectionPool
连接只能在单线程应用程序/脚本中使用。
ThreadedConnectionPool
可以在多线程应用程序/脚本中安全地使用连接。
这里是什么safely
意思?
我的理解/困惑:
"""
eg1: Simple Connection Pooling example
"""
from psycopg2.pool
from concurrent.futures
def someTask(id):
# CRUD queries to Postgres, that I will be multithreading
print(f"Thread: {id}")
conn = simple_pool.getconn()
# do DB operation
simple_pool = psycopg2.pool.SimpleConnectionPool(10, 15, #DB Info)
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
executor.map(someTask, range(1,10))
"""
eg2: Threaded Connection Pooling example
"""
from psycopg2.pool
from concurrent.futures
def someTask(id):
# CRUD queries to Postgres, that I will be multithreading
print(f"Thread: {id}")
conn = threaded_pool.getconn()
# do DB operation
threaded_pool = psycopg2.pool.ThreadedConnectionPool(10, 15, #DB Info)
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
executor.map(someTask, range(1,10))
Q1:我可能理解不正确,但在 eg1 中,someTask()
函数将按线程调用,所以如果它是简单的连接池,这将出错/将是UNSAFE(这是什么意思?)。
Q2:在 eg2 中,如果示例没问题, THREAD SAFE是什么意思,该someTask()
函数将被允许从池中获取连接,而在 eg1 中则不会?
Q3:两者在性能上有区别吗?
非常感谢我可以阅读以更好地理解这一点的任何其他资源/文章/文本。谢谢你。
解决方案
推荐阅读
- security - 在插入使用 TDE 加密的数据库之前加密 SSN 是否过大?
- angular - Angular 2+:如何从父组件提交搜索表单并在子组件中显示搜索结果?
- r - 我无法根据字符扩展行
- html - 我如何将文件输入链接到对象嵌入的源
- linux - 在使用 find 和 tar 归档文件时,归档会在过程中重置
- vb.net - 在类库中运行长时间操作并报告进度
- architecture - 如果没有实用程序微服务就无法运行,那么实用程序微服务是否应该/可以与所有依赖项合并?
- jquery - CheckBox 事件未触发
- javascript - 同一页面上的相同 Vue 组件两次重复组件视图
- jquery - 如何正确设置 div 复选标记的条件?