简体   繁体   English

测试 Python 任务或未来是否成功完成的最干净的方法是什么?

[英]What is the cleanest way to test if a Python Task or Future has completed successfully?

What is the cleanest way to test that a python Task or Future has completed successfully (neither canceled nor failed)?测试 python TaskFuture是否成功完成(既没有取消也没有失败)的最干净的方法是什么? This is the best I have come up with:这是我想出的最好的:

success = future.done() and not future.cancelled() and future.exception() is None

and it seems clumsy and error-prone, because it is easy to forget the not future.cancelled() .而且它看起来很笨拙且容易出错,因为很容易忘记not future.cancelled()

It is looking good for me.它看起来对我很好。 You can wrap in a function if you prefer:如果您愿意,可以使用 function 包装:

def is_success(future):
   return future.done() and not future.cancelled() and future.exception() is None

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

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