【问题标题】:ThreadPoolExecutor is not defined [python3]未定义 ThreadPoolExecutor [python3]
【发布时间】:2014-08-13 02:00:21
【问题描述】:

我正在尝试运行以下代码,该代码是直接从文档中复制的:https://docs.python.org/dev/library/concurrent.futures.html#module-concurrent.futures

import executor
import concurrent.futures
import time

def wait_on_b():
    time.sleep(5)
    print(b.result()) # b will never complete because it is waiting on a.                                
    return 5

def wait_on_a():
    time.sleep(5)
    print(a.result()) # a will never complete because it is waiting on b.                                
    return 6


executor = ThreadPoolExecutor(max_workers=2)
a = executor.submit(wait_on_b)
b = executor.submit(wait_on_a)

我得到以下输出:

Traceback (most recent call last):
  File "test1.py", line 16, in <module>
    executor = ThreadPoolExecutor(max_workers=2)
NameError: name 'ThreadPoolExecutor' is not defined

我假设我忘了导入一些东西,但我不知道。

【问题讨论】:

    标签: python multithreading python-3.x python-3.4


    【解决方案1】:

    要么使用from concurrent.futures import ThreadPoolExecutor 而不是import concurrent.futures,要么保持原样导入并使用executor = concurrent.futures.ThreadPoolExecutor(maxworkers=2)

    另请注意,您复制的示例代码旨在死锁,因此一旦您修复导入问题,它就无法正常工作。

    【讨论】:

      猜你喜欢
      • 2016-12-01
      • 2021-11-10
      • 1970-01-01
      • 2021-09-30
      • 2019-07-08
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多