【问题标题】:How to pass list as a arguments to a ThreadPoolExecutor and get the index and value如何将列表作为参数传递给 ThreadPoolExecutor 并获取索引和值
【发布时间】:2021-10-16 02:49:02
【问题描述】:

如何将列表传递给 ThreadPoolExecutor 并获取索引和值。我尝试过,但无法获得所需的输出。我想创建具有值和索引的多线程视频转换器将创建单独的文件夹,为此我该如何实现?

from concurrent.futures import ThreadPoolExecutor

def main(videos):
    transcode = Transcode()
    for index, video in enumerate(videos):
        with ThreadPoolExecutor(max_workers=4) as executor:
            executor.submit(transcode.hls, video, index)

【问题讨论】:

  • transcode.hls() 是否没有在 videoindex 的单独线程中被调用?目前尚不清楚您看到的行为与预期的行为。
  • 您能否提供Transcode 对象的代码,或者至少提供一些复制其行为的模型?

标签: python multithreading threadpoolexecutor


【解决方案1】:

您似乎想对存储在 Iterable 中的多个输入执行相同的工作 (transcode.hsl)。有一个方便的方法:

from concurrent.futures import ThreadPoolExecutor

def main(videos):
    transcode = Transcode()
    with ThreadPoolExecutor(max_workers=4) as executor:
        executor.map(transcode.hls, videos, range(len(videos))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 2013-11-06
    相关资源
    最近更新 更多