簡體   English   中英

未定義ThreadPoolExecutor [python3]

[英]ThreadPoolExecutor is not defined [python3]

我正在嘗試運行以下代碼,這些代碼是直接從文檔中復制的: 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

我以為我忘了導入一些東西,但是我不知道。

可以使用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)

還要注意,您復制的示例代碼旨在死鎖,因此一旦解決了導入問題,它將無法正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM