How To Get The Name Of The Analyzed Completed Future In Concurrent.futures?
While looking for an answer to my question (a solution is available in one answer in the linked duplicate), I discovered concurrent.futures, and specifically concurrent.futures.as_
Solution 1:
A possible solution is to use zip()
and create an iterator of (name, handler) tuples:
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")
Post a Comment for "How To Get The Name Of The Analyzed Completed Future In Concurrent.futures?"