阅读 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))
我的理解:
事实上,我从未得到想要的结果,而是收到消息“函数花费了超过 10 秒”。
你能告诉我为了获得预期结果我错过了什么(即 foo + bar)吗?
pebble
不支持python 3.2,我建议升级到大于或等于3.6的版本(当前最低支持版本)。
来源https://github.com/noxdafox/pebble/blob/master/setup.py#L27
python_requires=">=3.6",