ThreadPoolExecutor 在函数内部调用时不使用多个 Worker

ThreadPoolExecutor Not Using Multiple Workers When Being Called Inside a Function

我正在尝试制作一个扫描给定 IP 地址的模块,并根据每个端口的当前状态 returns 为真或假。当上下文管理器被自己调用时,它工作正常,但当它在函数中被调用时,它会停止使用所有分配给它的线程。这是我的代码:

import socket
import concurrent.futures

def _scan(ip, port):
    scanner = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    scanner.settimeout(1)
    try:
        scanner.connect((ip, port))
        scanner.close()
        return True
    except:
        return False

def portScan(ip, workers, portNum):
    with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor:
        for port in range(portNum):
            future = executor.submit(_scan, ip, port + 1)
            print(future.result())

portScan("1.1.1.1", 100, 1000)

问题是您在等待每个未来完成后再提交下一个。您可以改用 map。它会将工作分散到所有线程,并按照与提交的参数相同的顺序迭代结果。

import socket
import concurrent.futures

def _scan(params):
    ip, port = params
    scanner = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    scanner.settimeout(1)
    try:
        scanner.connect((ip, port))
        scanner.close()
        return True
    except:
        return False


def portScan(ip, workers, portNum):
    with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor:
        for result in executor.map(_scan, ((ip, port) for port in range(portNum))):
            print(result)

portScan("127.0.0.1", 5, 22)