python - 当 Python 并发未来对象完成时,如何停止主线程而不进行轮询?
问题描述
解决方案
Instead of polling for the result you should use concurrent.futures.as_completed to loop through the results:
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
futures = []
futures.append(executor.submit(function, *args, **kwargs))
for future in concurrent.futures.as_completed(futures):
result = f.result()
if f.exception() or not result[0]:
c = "✗"
else:
c = "✔"
print(f"\33[2K\r{phrase} {c}")
You can find the documentation here: Futures doc The function as_completed an iterator to go through all finished futures, which has ended successfully.
You need to adapt your bar_length approach, but hopefully, this helps you to get another idea of how to wait for your results.
推荐阅读
- ssh - 无法通过 SSH 访问 GCP 实例
- java - 没有 android 目标和 com.android.sdklib.tool.AvdManagerCli (cordova build android)
- php - 为什么 json_encode 在 PHP 7.0 中不起作用,但在 PHP 7.3 中起作用?
- apache-spark - 如何在 Spark SQL Query 中加速 WholeStageCodegen?
- java - 地图在 OpenStreetMap (osm) 中不可见
- java - 如何在没有工件或 setting.xml 的情况下使用 Maven 项目?
- java - 如何列出特定jar文件中的所有包?
- maven - 从依赖项加载资源
- python - Power BI:如何使用 Python 处理/清理字符串数据?
- flutter - 删除 Flutter 中的导航覆盖