如何在Python中为ThreadPoolExecutor线程赋予不同的名称 [英] How to give different names to ThreadPoolExecutor threads in Python
问题描述
我有下面的代码用于创建线程并运行它们.
I have the below code for creating threads and running them.
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机器上运行代码时,这是生成的输出:
When i run the code in my windows 10 machine this is the output which gets generated:
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
我们看到两个线程都具有相同的名称.我如何通过给它们起不同的名字来区分它们?这以某种方式是并发类的功能吗?
As we see both the threads have the same name. How do i differentiate between them by giving them different names ? Is this somehow a feature of the concurrent.futures class ?
非常感谢您的回答.
推荐答案
"rel ="nofollow noreferrer">文档:
from the docs:
3.6版中的新增功能:添加了thread_name_prefix参数以允许用户控制线程.由池创建的辅助线程的线程名称可简化调试.
New in version 3.6: The thread_name_prefix argument was added to allow users to control the threading.Thread names for worker threads created by the pool for easier debugging.
这篇关于如何在Python中为ThreadPoolExecutor线程赋予不同的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!