gpt4 book ai didi

python - 如何将线程对象传递给 ThreadPoolExecutor?

转载 作者:行者123 更新时间:2023-12-05 07:15:39 27 4
gpt4 key购买 nike

concurrent.futures.ThreadPoolExecutor 通过 传递 function 到执行器 executor.submit(my_function) 像这样:

def my_function():
print("Hello, world!")

with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
for t in range(50):
executor.submit(my_function())

但是有没有办法传递一个线程对象来代替?我有一个线程子类,我想与执行程序一起使用。

最佳答案

尝试这样的事情:

import concurrent.futures

def my_function(num_time):
print(f"Hello, world {num_time} times")

with concurrent.futures.ThreadPoolExecutor() as executor:

results = [executor.submit(my_function,i) for i in range(50)]


for f in concurrent.futures.as_completed(results):
print(f.result())

关于python - 如何将线程对象传递给 ThreadPoolExecutor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59555930/

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