Python3中使用concurrent.futures线程池识别线程运行函数

问题描述 投票:0回答:1

我有以下代码:

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

我想要的是能够将“线程x”替换为“x”“线程ID/编号”。结果应该是这样的: Thread 0 => abcd xpto Thread 1 => abcd xpto Thread 2 => abcd xpto

第一个问题是传递多个参数,但使用 lambda 我能够做到这一点。

python python-3.x concurrent.futures
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.