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?
(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.