简体   繁体   English

未定义ThreadPoolExecutor [python3]

[英]ThreadPoolExecutor is not defined [python3]

I'm trying to run the following code, which was directly copied from the documentation: https://docs.python.org/dev/library/concurrent.futures.html#module-concurrent.futures : 我正在尝试运行以下代码,这些代码是直接从文档中复制的: 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)

And I get the following output: 我得到以下输出:

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

I'm assuming that I forgot to import something, but I don't know. 我以为我忘了导入一些东西,但是我不知道。

Either use from concurrent.futures import ThreadPoolExecutor instead of import concurrent.futures , or leave the import as-is and use executor = concurrent.futures.ThreadPoolExecutor(maxworkers=2) . 可以使用executor = concurrent.futures.ThreadPoolExecutor(maxworkers=2) from concurrent.futures import ThreadPoolExecutor而不是executor = concurrent.futures.ThreadPoolExecutor(maxworkers=2)进行import concurrent.futures ,也可以直接使用from concurrent.futures import ThreadPoolExecutor并使用executor = concurrent.futures.ThreadPoolExecutor(maxworkers=2)

Also note that the example code you copied is designed to deadlock, so it's not going to work properly once you fix the import issue. 还要注意,您复制的示例代码旨在死锁,因此一旦解决了导入问题,它将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM