7

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()

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 ?

Many thanks for any answers.

Subhayan Bhattacharya
  • 5,407
  • 7
  • 42
  • 60
  • Since it seems this is not currently supported by the futures api itself, inside your callable that you provide on the `submit`, try: `threading.current_thread().setName()`. – matanster Aug 14 '20 at 18:33

2 Answers2

5

from the docs:

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.

using the thread_name_prefix argument:

concurrent.futures.ThreadPoolExecutor(max_workers=None, thread_name_prefix='')

齐思贤
  • 3
  • 2
ospider
  • 9,334
  • 3
  • 46
  • 46
  • 5
    Is there a way to access this `thread_name_prefix`? I want to access this name and make it a part of threads` local memory. – ashutosh singh Jan 22 '20 at 06:28
1

You say: "both the threads have the same name".
That's not correct! The name is the same because the same thread is used for both tasks: in fact task() exits immediately.
In order to have both threads involved, you have to add some sleep in your task() function.

Just to recap:

(1) without sleep:

from concurrent.futures import ThreadPoolExecutor
import threading
import time

def task(n):
    result = 0
    i = 0
    for i in range(n): result = result + i
    print(f'{threading.current_thread().name} with variable {n}: {result}')
    
executor = ThreadPoolExecutor(max_workers=3)
executor.submit(task, (10))
executor.submit(task, (100))    

In this case the output will be:

ThreadPoolExecutor-0_0 with variable 10: 45 ThreadPoolExecutor-0_0 with variable 100: 4950

(2) with a sleep inside task(), to make the function longer in time:

from concurrent.futures import ThreadPoolExecutor
import threading
import time

def task(n):
    result = 0
    i = 0
    for i in range(n): result = result + i
    time.sleep(.5)
    print(f'{threading.current_thread().name} with variable {n}: {result}')

executor = ThreadPoolExecutor(max_workers=3)
executor.submit(task, (10))
executor.submit(task, (100)) 

In this case the output will be:

ThreadPoolExecutor-0_0 with variable 10: 45 ThreadPoolExecutor-0_1 with variable 100: 4950

bilbo
  • 111
  • 1
  • 3