【问题标题】:executor.map() TypeError: zip argument #2 must support iterationexecutor.map() TypeError: zip 参数 #2 必须支持迭代
【发布时间】:2013-04-01 12:55:02
【问题描述】:

executor.map() TypeError: zip 参数 #2 必须支持迭代

当我运行它时,刚刚生成 TypeError: zip argument #2 must support iteration。 谁能帮我解决这个问题?

import time, concurrent.futures
lst100=[i for i in range(100)]

t1=time.clock()
print(list(map(str,lst100)))
t2=time.clock()
print(t2-t1)

t3=time.clock()
with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
    future_to_url = executor.map(str,lst100, 60)
    print(list(future_to_url))
t4=time.clock()
print(t4-t3)

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    concurrent.futures.Executor.map 在语义上与内置函数map 相同。第二个和后续的 非关键字 参数指定将给定函数应用到的可迭代对象。

    在您的情况下,您是说“这里有两个可迭代对象:100 个元素的列表 (lst100) 和整数 60。请在每对元素 a 和 @987654328 上调用函数 str() @ 分别形成两个可迭代对象,并返回结果列表。”但由于整数 60 实际上不是一个可迭代对象,所以它失败了。

    假设您想指定 60 秒的超时时间,您需要将其作为关键字参数传递,如下所示:

    future_to_url = executor.map(str, lst100, timeout=60)
    

    关键字参数与位置参数的区别在于在传递值之前存在name= 前缀。在这种情况下,参数名称是timeout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-12
      • 2020-10-27
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 2015-06-01
      • 2019-09-11
      • 1970-01-01
      相关资源
      最近更新 更多