Search code examples
pythonpython-3.xconcurrent.futures

Identifying thread running function using concurrent.futures thread pool in Python3


I have the following code:

import concurrent.futures

def f(a, b):
    print("Thread x => "+a+" "+b)

with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
   for n in executor.map(lambda n: f('abcd', 'xpto'), range(3)):
      pass

What I want is to be able to replace the "Thread x" being the "x" the "thread id/number". The result should be like this:

Thread 0 => abcd xpto
Thread 1 => abcd xpto
Thread 2 => abcd xpto

The first issue was to pass multiple arguments, but using lambda I was able to do it.


Solution

  • Did you mean to pass x as a parameter?

    import concurrent.futures
    
    def f(x, a, b):
        print("Thread”, x, “=>"+a+" "+b)
    
    with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
       for n in executor.map(lambda n: f(n, 'abcd', 'xpto'), range(3)):
          pass