如何在并发.futures中获得已分析完成的未来的名称?

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

[在寻找问题的答案时(链接副本中一个答案中有solution,我发现了concurrent.futures,尤其是concurrent.futures.as_completed

我最终得到的代码(在上下文中:我需要捕获主程序从该主程序启动的线程中引发的异常)工作正常,除了我不知道如何重新附加到该程序的名称之外在concurrent.futures.as_completed中检查的功能:

concurrent.futures.as_completed

在成功/失败输出中,我知道某个方法会失败或不是异常。我不知道是哪个做了什么。

由于迭代必须在线程处理程序上进行,我找不到在import concurrent.futures class Checks: @staticmethod def isok(): print("OK") @staticmethod def isko(): raise Exception("KO") # db will keep a map of method names in Check with the thread handler db = {} with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: # get the names of the methods in Check, discarding the built-ion ones for check in [k for k in dir(Checks) if not k.startswith('_')]: db[check] = executor.submit(getattr(Checks, check)) # at that point I have a map of function_name_as_a_string -> thread handler for future in concurrent.futures.as_completed([db[k] for k in db.keys()]): if future.exception(): print(f"{str(future.exception())} was raised (I do not know where, somewhere)") else: print("success (again, I do not know where, exactly)") # all the threads are finished at that point print("all threads are done") 中将其“注入”循环中使用方法名称的方法-我该怎么做?

python python-3.x concurrent.futures
1个回答
0
投票

一种可能的解决方案是使用print()并创建(名称,处理程序)元组的迭代器:

zip()
© www.soinside.com 2019 - 2024. All rights reserved.