英文:
How to wait but not block thread when getting a resource in python?
问题
假设我们需要创建一个数据库连接池,要求是当客户端尝试获取连接时,如果所有现有的连接都在忙碌状态,那么需要等待30秒后放弃,希望其他客户端释放一些连接。因此,一个简单的解决方案是:
def get_connection():
if 所有连接都在忙碌:
time.sleep(30)
再次尝试获取连接
else:
return 连接
但由于 time.sleep(30)
会阻塞线程,如果两个客户端同时尝试获取连接,它将阻塞60秒。那么,是否有一种不阻塞但又能等待一段时间的方式呢?
英文:
Suppose we need to create a database connection pool, the requirement is that when a client tries to get a connection, if all exisiting connections are busy, then need to wait for 30 second before giving up, hope some connections are released by other client. So the naive solution is
def get_connection():
if all_conn_are_busy:
time.sleep(30)
try to get connection again
else:
return conn
But since time.sleep(30) will block the thread, if 2 clients trying to get connection at the same time, it will block for 60 seconds. So is there any way to noblock it but also wait for some time?
答案1
得分: 2
(A.) 如果你处于这种“等待”模式,那么你已经输掉了这场战斗。
(B.) 你似乎希望有一个专门用于获取下一个连接的单线程。如果它已经忙于尝试获取一个连接,那么第二个调用者应该立即遭受致命错误。
拥有数十个线程都争相从一个超载的数据库服务器获取下一个连接听起来像是一场灾难的处方。
考虑以较慢的速率预分配连接,这样总是有一个可供下一个请求者使用。
显然,你有一些连接池和丢弃空闲连接的策略。重新评估这个策略在你当前的使用情况下是否合适。
英文:
(A.) You have already lost the battle if you're in this "waiting" regime.
(B.) It sounds like you want to have a single thread which is exclusively
dedicated to acquiring that next connection.
And if it is already busy trying to acquire one,
then 2nd caller should promptly suffer fatal error.
To have dozens of threads all clamoring for next connection
from a terminally overloaded DB server sounds like a recipe for disaster.
Consider pre-allocating connections at some slow rate,
so there is always one available for the next requestor.
Apparently you have some connection pool, and a policy
for discarding idle connections. Re-evaluate how well suited
that policy is in light of your current use cases.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论