如何将从文件读取的工作划分为线程?

时间:2019-04-30 13:50:01

标签: python multithreading

我有一个主机列表和一个端口列表。 我想运行X线程,每次将捕获一个主机,并尝试连接到这些端口。 我已经覆盖了连接部分,我的问题是如何将其穿线。 当我做一些研究时,我真的在线程,多进程和异步之间感到困惑。 最佳和/或最简单的库是什么?

现在我的伪代码没有线程代码是:

def getHosts():
    hosts = open("hostList.txt").read().splitlines()
    return hosts
def getPorts():
    ports = open("portList.txt").read().splitlines()

hosts = getHosts()
ports = getPorts()
for host in hosts
    for port in ports
        ---the connection code---

我认为我的一般想法是获取列表的长度,将其除以线程数,然后创建一个线程,该线程将从thread_number * result_of_divide一直运行到(thread_number + 1)* result_of_divide。

1 个答案:

答案 0 :(得分:1)

ThreadPoolExecutor

import concurrent.futures

MAX_THREAD_COUNT = 10


def read_text_file(filename):
    with open(filename, "r") as f:
        return f.read().splitlines()


def check_port(host, port):
    pass


hosts = read_text_file("hostList.txt")
ports = read_text_file("portList.txt")


with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_THREAD_COUNT) as executor:
    results = {}
    for host in hosts:
        for port in ports:
            results[executor.submit(check_port, host, port)] = "{}:{}".format(host, port)

for result in concurrent.futures.as_completed(results):
    host_and_port = results[result]
    try:
        returned_data = result.result()
    except Exception as e:
        print("\"{}\" exception have been caused while checking {}".format(e, host_and_port))
    else:
        print("Checking of {} returned {}".format(host_and_port, returned_data))

P.S。。代码可能不是100%正确,我还没有对其进行检查。