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

问题描述 投票:0回答:1

阅读 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 / exceptelse“分支”并获得打印结果,即“3”

事实上,我从未得到想要的结果,而是收到消息“函数花费了超过 10 秒”。

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

python concurrency timeout
1个回答
0
投票

pebble
不支持python 3.2,我建议升级到大于或等于3.6的版本(当前最低支持版本)。

来源https://github.com/noxdafox/pebble/blob/master/setup.py#L27

    python_requires=">=3.6",
© www.soinside.com 2019 - 2024. All rights reserved.