【问题标题】:Identifying thread running function using concurrent.futures thread pool in Python3Python3中使用concurrent.futures线程池识别线程运行函数
【发布时间】:2020-12-28 16:31:40
【问题描述】:

我有以下代码:

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

我想要的是能够将 "Thread x" 替换为 "x" 的 "thread id/number"。结果应该是这样的:

线程 0 => abcd xpto

线程 1 => abcd xpto

线程 2 => abcd xpto

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

提前致谢,

BR

【问题讨论】:

    标签: python python-3.x concurrent.futures


    【解决方案1】:

    您的意思是将 x 作为参数传递吗?

    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
    

    【讨论】:

    • 是的,就这样。我是 python 新手,所以有很多我不知道 :) 感谢您对这个简单问题的快速回答。 BR
    猜你喜欢
    • 2013-03-16
    • 2019-12-29
    • 1970-01-01
    • 2023-03-12
    • 2011-09-29
    • 2015-10-18
    • 2011-11-23
    • 2019-01-29
    相关资源
    最近更新 更多