如何在 concurrent.futures 中获取已分析的已完成未来的名称?
How to get the name of the analyzed completed future in concurrent.futures?
在寻找我的问题的答案时( is available in one answer in the linked duplicate), I discovered concurrent.futures
, and specifically 。
我最终得到的代码(对于上下文:我需要捕获从该主程序启动的线程中引发的主程序异常)工作正常,除了我不知道如何重新附加到在 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")
在 success/failure 输出中,我知道方法 raisod 或不是异常。不知道是谁做了什么。
由于迭代必须在线程处理程序上进行,所以我找不到方法 "inject" 循环中的方法名称以在 print()
中使用它 - 如何我可以这样做吗?
一个可能的解决方案是使用 zip()
并创建一个(名称,处理程序)元组的迭代器:
import concurrent.futures
class Checks:
@staticmethod
def isok():
print("OK")
@staticmethod
def isko():
raise Exception("KO")
# db will keep a map of method namles in Check with the actual (executable) method
db = {}
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
for check in [k for k in dir(Checks) if not k.startswith('_')]:
db[check] = executor.submit(getattr(Checks, check))
for name, handler in zip(db.keys(), concurrent.futures.as_completed([db[k] for k in db.keys()])):
if handler.exception():
print(f"{str(handler.exception())} was raised by {name}")
else:
print(f"success in {name}")
# all the threads are finished at that point
print("all threads are done")
在寻找我的问题的答案时(concurrent.futures
, and specifically
我最终得到的代码(对于上下文:我需要捕获从该主程序启动的线程中引发的主程序异常)工作正常,除了我不知道如何重新附加到在 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")
在 success/failure 输出中,我知道方法 raisod 或不是异常。不知道是谁做了什么。
由于迭代必须在线程处理程序上进行,所以我找不到方法 "inject" 循环中的方法名称以在 print()
中使用它 - 如何我可以这样做吗?
一个可能的解决方案是使用 zip()
并创建一个(名称,处理程序)元组的迭代器:
import concurrent.futures
class Checks:
@staticmethod
def isok():
print("OK")
@staticmethod
def isko():
raise Exception("KO")
# db will keep a map of method namles in Check with the actual (executable) method
db = {}
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
for check in [k for k in dir(Checks) if not k.startswith('_')]:
db[check] = executor.submit(getattr(Checks, check))
for name, handler in zip(db.keys(), concurrent.futures.as_completed([db[k] for k in db.keys()])):
if handler.exception():
print(f"{str(handler.exception())} was raised by {name}")
else:
print(f"success in {name}")
# all the threads are finished at that point
print("all threads are done")