我有以下代码用于创建线程并运行它们。

from concurrent.futures import ThreadPoolExecutor
import threading


def task(n):
    result = 0
    i = 0
    for i in range(n):
        result = result + i
    print("I: {}".format(result))
    print(f'Thread : {threading.current_thread()} executed with variable {n}')

def main():
    executor = ThreadPoolExecutor(max_workers=3)
    task1 = executor.submit(task, (10))
    task2 = executor.submit(task, (100))

if __name__ == '__main__':
    main()

当我在Windows 10机器中运行代码时,这是生成的输出:
I: 45
Thread : <Thread(ThreadPoolExecutor-0_0, started daemon 11956)> executed with variable 10
I: 4950
Thread : <Thread(ThreadPoolExecutor-0_0, started daemon 11956)> executed with variable 100

Process finished with exit code 0

如我们所见,两个线程具有相同的名称。我如何通过给它们起不同的名字来区分它们?这是否是并发类的功能?

非常感谢您提供任何答案。

最佳答案

来自docs:

关于python-3.x - 如何在Python中为ThreadPoolExecutor线程赋予不同的名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51439283/

10-10 21:31