gpt4 book ai didi

python - 识别 concurrent.futures.ThreadPoolExecutor 中的当前线程

转载 作者:行者123 更新时间:2023-12-05 02:40:16 25 4
gpt4 key购买 nike

下面的代码有 5 个 worker ....每个人都打开自己的 worker_task()

with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_url = {executor.submit(worker_task, command_, site_): site_ for site_ in URLS}

for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try: data = future.result()

但是 ..... 在每个 worker_task() 中......我无法识别......5 个 worker 中的哪一个 当前正在使用( Worker_ID)

如果我想在 worker_task()print('worker 3 has finished') .....我不能这样做,因为 executor.submit 不允许

有什么解决办法吗?

最佳答案

您可以借助 threading.current_thread() 函数获取工作线程的名称。请在下面找到一些示例:

from concurrent.futures import ThreadPoolExecutor, Future
from threading import current_thread
from time import sleep
from random import randint

# imagine these are urls
URLS = [i for i in range(100)]


def do_some_work(url, a, b):
"""Simulates some work"""
sleep(2)
rand_num = randint(a, b)
if rand_num == 5:
raise ValueError("No! 5 found!")
r = f"{current_thread().getName()}||: {url}_{rand_num}\n"
return r


def show_fut_results(fut: Future):
"""Callback for future shows results or shows error"""
if not fut.exception():
print(fut.result())
else:
print(f"{current_thread().getName()}| Error: {fut.exception()}\n")


if __name__ == '__main__':
with ThreadPoolExecutor(max_workers=10) as pool:
for i in URLS:
_fut = pool.submit(do_some_work, i, 1, 10)
_fut.add_done_callback(show_fut_results)

如果你想更多地控制线程,使用threading模块:

from threading import Thread
from queue import Queue
from time import sleep
from random import randint
import logging

# imagine these are urls
URLS = [f"URL-{i}" for i in range(100)]

# number of worker threads
WORKER_NUM = 10


def do_some_work(url: str, a: int, b: int) -> str:
"""Simulates some work"""
sleep(2)
rand_num = randint(a, b)
if rand_num == 5:
raise ValueError(f"No! 5 found in URL: {url}")
r = f"{url} = {rand_num}"
return r


def thread_worker_func(q: Queue, a: int, b: int) -> None:
"""Target function for Worker threads"""
logging.info("Started working")
while True:
try:
url = q.get()

# if poison pill - stop worker thread
if url is None:
break

r = do_some_work(url, a, b)
logging.info(f"Result: {r}")
except ValueError as ex:
logging.error(ex)
except Exception as ex:
logging.error(f"Unexpected error: {ex}")

logging.info("Finished working")


if __name__ == '__main__':
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s | %(threadName)s | %(asctime)s | %(message)s",
)
in_q = Queue(50)
workers = [
Thread(target=thread_worker_func, args=(in_q, 1, 10, ), name=f"MyWorkerThread-{i+1}")
for i in range(WORKER_NUM)
]
[w.start() for w in workers]

# start distributing tasks
for _url in URLS:
in_q.put(_url)

# send poison pills to worker-threads
for w in workers:
in_q.put(None)

# wait worker thread to join Main Thread
logging.info("Main Thread waiting for Worker Threads")
[w.join() for w in workers]

logging.info("Workers joined")
sleep(10)
logging.info("App finished")

关于python - 识别 concurrent.futures.ThreadPoolExecutor 中的当前线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68647962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com