Python-Pebble-对超时功能的误解

时间:2019-03-03 17:28:40

标签: python concurrency timeout

在阅读了Pebble的文档(https://pythonhosted.org/Pebble/)之后,这似乎很简单。但是,我无法获得预期的结果。

我从他们网站上给出的例子开始

import time
from pebble import concurrent, ProcessExpired
from concurrent.futures import TimeoutError

@concurrent.process(timeout=10)
def function(foo, bar=0):
    time.sleep(5)
    return foo + bar

future = function(1, bar=2)

try:
    result = future.result()
except TimeoutError as error:
    print("function took longer than %d seconds" % error.args[1])
except ProcessExpired as error:
    print("%s. Exit code: %d" % (error, error.exitcode))
except Exception as error:
    print("function raised %s" % error)
    print(error.traceback)
else:
    print(str(result))

我的理解:

  1. 该功能已设置10秒钟的超时时间
  2. future.result()阻塞,直到结果准备就绪
  3. 该函数执行大约需要5秒钟
  4. 您将直接转到 try /除外 else “分支”并获取打印的内容结果就是“ 3”

实际上,我从未获得理想的结果,而是显示消息“功能花费了10秒钟以上”。

您能告诉我我错过了什么才能获得预期的结果吗(即foo + bar)?

0 个答案:

没有答案