简体   繁体   English

gevent池wait_available线程安全吗

[英]Is gevent pool wait_available thread safe

So I have a function lets say test and gevent pool of size 10. 所以我有一个功能可以说是测试和大小为10的gevent池。

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_available should be thread safe but the pool.spawn will it have thread safety/race condition issue. 我的意思是wait_available应该是线程安全的,但是pool.spawn会出现线程安全/竞争条件问题。 I mean lets say there are already 9 greenlets running and there a couple of requests calling the test function. 我的意思是说已经有9个greenlet正在运行,并且有几个请求调用了test函数。 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. 他们两个都将读取不应该阻止的pool.wait_available,但是紧随其后的pool.spawn将使其中之一成为阻止。

I just want to make sure that the pool.spawn doesn't block for more than the specified timeout period. 我只想确保pool.spawn不会超过指定的超时时间。 How can I accomplish this? 我该怎么做?

wait_available isn't necessary if you're using spawn from pool because spawn will ask for a lock in the Pool s internal semaphore that is used to track running greenlets. 如果您从pool使用spawn ,则不需要wait_available ,因为spawn会要求lock Pool的内部信号量,该信号量用于跟踪正在运行的greenlet。 An exception to this is only when you use apply_async . 仅当您使用apply_asyncapply_async I will explain both scenarios here: 我将在这里解释两种情况:

pool.Pool(size=10)
def test():
    for i in xrange(20):
        log('Processing {}'.format(i))
        pool.spawn(something)
    pool.join()
    log('Done')

The output of this shows it will spawn greenlets in groups of 10 since the pool contains space for 10: 此输出显示由于池包含10个空间,因此将以10个为一组生成greenlets:

1531531331: Processing 0
1531531331: Processing 1
1531531331: Processing 2
1531531331: Processing 3
1531531331: Processing 4
1531531331: Processing 5
1531531331: Processing 6
1531531331: Processing 7
1531531331: Processing 8
1531531331: Processing 9
1531531340: Processing 10
1531531340: Processing 11
1531531340: Processing 12
1531531340: Processing 13
1531531340: Processing 14
1531531340: Processing 15
1531531340: Processing 16
1531531340: Processing 17
1531531340: Processing 18
1531531340: Processing 19
1531531349: Done

Conversely, if you use apply_Async instead of spawn, it will force all the calls to run at the same time. 相反,如果使用apply_Async而不是spawn,它将强制所有调用同时运行。 There will be a race condition here for all greeenlets to start execution immediately. 在这里,所有的greeenlet都将出现竞争条件,以立即开始执行。

1531531357: Processing 0
1531531357: Processing 1
1531531357: Processing 2
1531531357: Processing 3
1531531357: Processing 4
1531531357: Processing 5
1531531357: Processing 6
1531531357: Processing 7
1531531357: Processing 8
1531531357: Processing 9
1531531357: Processing 10
1531531357: Processing 11
1531531357: Processing 12
1531531357: Processing 13
1531531357: Processing 14
1531531357: Processing 15
1531531357: Processing 16
1531531357: Processing 17
1531531357: Processing 18
1531531357: Processing 19
1531531367: Done

If you use wait_available() at the beginning, you go back to similar behaviour to spawn . 如果在开始时使用wait_available() ,则返回类似的行为spawn So, if you are using spawn, you dont need wait_available() as they do same check(checking the semaphore to see if there's any room in the pool). 因此,如果您使用的是spawn,则不需要wait_available()因为它们会执行相同的检查(检查信号量以查看池中是否有空间)。

Hope it helps! 希望能帮助到你! Gevent is amazing! Gevent很棒! happy coding! 祝您编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM