So I have a function lets say test and gevent pool of size 10.
pool.Pool(size=10)
def test():
pool.wait_available(timeout=0.5)
pool.spawn(something)
If the function test is called by different threads, will it cause any issue. I mean wait_availableshould be thread safe but the pool.spawn will it have thread safety/race condition issue. I mean lets say there are already 9 greenlets running and there a couple of requests calling the test function. Both of them will read pool.wait_available which should not block But the pool.spawn right after that will make one of them block.
I just want to make sure that the pool.spawn doesn't block for more than the specified timeout period. How can I accomplish this?
wait_availableisn't necessary if you're usingspawnfrompoolbecausespawnwill ask for alockin thePools internal semaphore that is used to track running greenlets. An exception to this is only when you useapply_async. I will explain both scenarios here:The output of this shows it will spawn greenlets in groups of 10 since the pool contains space for 10:
Conversely, if you use
apply_Asyncinstead of spawn, it will force all the calls to run at the same time. There will be a race condition here for all greeenlets to start execution immediately.If you use
wait_available()at the beginning, you go back to similar behaviour tospawn. So, if you are using spawn, you dont needwait_available()as they do same check(checking the semaphore to see if there's any room in the pool).Hope it helps! Gevent is amazing! happy coding!