Geben Sie Hinweise für concurrent.futures.Executor-Unterklassen einPython

Python-Programme
Anonymous
 Geben Sie Hinweise für concurrent.futures.Executor-Unterklassen ein

Post by Anonymous »

Ich habe den folgenden Code in VSCode entwickelt, der ohne Fehler läuft:

Code: Select all

import random
import time
from concurrent.futures import (
InterpreterPoolExecutor,
ProcessPoolExecutor,
ThreadPoolExecutor,
Executor,
)
from os import process_cpu_count

def cpus() -> int:
"""
Get the number of available CPUs minus one but with a minimum of 2
"""
ncpus = process_cpu_count() or 2
return ncpus - 1 if ncpus > 3 else 2

def func(_: int) -> list[int]:
return [random.randint(1, 10) for _ in range(10_000)]

def process(pool: Executor) -> None:
start = time.perf_counter()
with pool() as e:
for _ in e.map(func, range(cpus())):
pass
duration = time.perf_counter() - start
print(pool.__name__, f"{duration=:.4f}s")

if __name__ == "__main__":
for pool_executor in (
InterpreterPoolExecutor,
ProcessPoolExecutor,
ThreadPoolExecutor,
):
process(pool_executor)
Das Problem, das ich habe, ist, dass Pylance sich über die Zeile beschwert:

Code: Select all

with pool() as e:
...zeigt an, dass „Der Objekttyp von Executor ist nicht aufrufbar“
Vermutlich liegt das daran, dass der Typhinweis von Executor falsch ist.
Wie überwinde ich das?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post