简体   繁体   English

无法使用python eventlet库(eventlet.timeout.Timeout)超时

[英]Not able to timeout using python eventlet library (eventlet.timeout.Timeout)

I am looping over a list and performing some action on each member of the list. 我正在遍历列表,并对列表的每个成员执行一些操作。 If a member takes too much time (1 sec in this case), I intend to pass it. 如果成员花费太多时间(在这种情况下为1秒),我打算通过它。 However the block inside the try statement is always being processed and is never timing out . 但是try语句中的块始终在处理,并且永远不会超时 I don't understand why. 我不明白为什么。

    from eventlet import *        

    for rule in data:

        #Timeout block
        t=Timeout(1)
        try:
            f = expr2bdd(expr(rule))
            solutions = satisfy_all(f, count=True)
            each_rule["solution"]=solutions
        except:
            pass
        finally:
            t.cancel()

Eventlet is a concurrent networking library... Eventlet是并发网络库...

It's not clear what expr2bdd and satisfy_all functions do, but most likely they only do some CPU calculations and no disk/network IO. 目前还不清楚是什么expr2bddsatisfy_all功能做的,但最有可能,他们只能做一些CPU的计算和无磁盘/网络IO。 In this case there is no point where Eventlet gets a chance to run and fire timeout exception. 在这种情况下,Eventlet没有机会运行并引发超时异常。

If you have control over expr2bdd and satisfy_all functions and there are any kind of loops, place eventlet.sleep(0) at each iteration. 如果你有控制expr2bddsatisfy_all功能,有任何形式的循环,地点eventlet.sleep(0)在每个迭代。 That's Eventlet idiom for "yield control to other coroutines", that's where timeout will be fired. 那是Eventlet的成语,意为“控制其他协程的产量”,这就是超时的地方。

If you don't have control over said functions, second best option is to run them in a separate process which you can forcefully kill. 如果您无法控制上述功能,则第二好的选择是在一个单独的进程中运行它们,您可以强制终止该进程。 On POSIX compatible OS (eg Linux, *BSD, OSX), you can use os.fork to run a piece of code in separate process. 在兼容POSIX的OS(例如Linux,* BSD,OSX)上,可以使用os.fork在单独的进程中运行一段代码。 For maximum portability, use subprocess.Popen([sys.executable,...]) or multiprocessing.Process . 为了获得最大的可移植性,请使用subprocess.Popen([sys.executable,...])multiprocessing.Process The latter gives higher level API, mainly around easier data exchange (serialization) at the cost of performance overhead, which may be negligible in your case. 后者提供了更高级别的API,主要是围绕简化数据交换(序列化)而付出的,而这是以性能开销为代价的,在您的情况下,这可以忽略不计。 In any case, basic pattern is this: (in a thread or eventlet coroutine, you start a second process and then .communicate()/join() on it. Use eventlet.Timeout or Thread.join() with timeout. If timeout fires, use p.terminate() or p.kill() to stop current calculations. 无论如何,基本模式是这样的:(在线程或事件let协程中,启动第二个进程,然后在其上执行eventlet.Timeout .communicate()/join() 。将eventlet.TimeoutThread.join()与超时一起使用。如果超时触发时,请使用p.terminate()p.kill()停止当前计算。

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

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