python - ThreadPoolExecutor 未定义 [python3]

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

我正在尝试运行以下代码,该代码是直接从文档复制的: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

我假设我忘记导入某些内容,但我不知道。

最佳答案

要么使用from并发.futures import ThreadPoolExecutor而不是import并发.futures,或者保持导入不变并使用executor =并发.futures。 ThreadPoolExecutor(maxworkers=2).

另请注意,您复制的示例代码被设计为死锁,因此一旦解决导入问题,它就无法正常工作。

关于python - ThreadPoolExecutor 未定义 [python3],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25276925/

相关文章:

python - Pickling 对象实例给出 TypeError : __new__() missing required positional arguments

C#线程图像处理

python请求post to url查询参数需要特殊字符

python - 将字符添加到python 3中的字符串

python - Python 中的 zipfile 生成不太正常的 ZIP 文件

python - tensorflow - 内存泄漏?

python - 将内存中的变量保存到文件中

python - 正则表达式数字数据处理: match a series of numbers greater than X

java - 防止在 Java 中创建/启动线程

java - 如何在所有线程完成时立即做某事?