简体   繁体   English

简单的python脚本,多线程?

[英]Simple python script, multithreaded?

I have a simple python script that reads from two .txt files and saves an output on a new text file.我有一个简单的 python 脚本,它从两个 .txt 文件中读取并将输出保存在一个新的文本文件中。

This is what my code looks like:这是我的代码的样子:

import os

with open("users.txt", encoding="utf-8") as f:
    users = f.readlines()
users = [x.strip() for x in users] 

with open("users1.txt", encoding="utf-8") as f:
    passes = f.readlines()
passes = [x.strip() for x in passes]

results = []

for user in users:
    h = user[user.find(':') + 1:]
    for p in passes:
        if p[:p.find(':')] == h:
            results.append((user[:user.find(':')], p[p.find(':') + 1:]))
            passes.remove(p)
            break

with open('results.txt', 'w+', encoding="utf-8") as f:
    for item in results:
        f.write(f"{item[0]}:{item[1]}\n")

The user.txt has about 3-10M lines.. Which of course will take ages to process, im talking 1-3 hours. user.txt 大约有 3-10M 行.. 当然这需要很长时间来处理,我说 1-3 个小时。 I thought the only reason of this is because the script is running in a single thread.我认为唯一的原因是脚本在单个线程中运行。 I did some digging around and im out of ideas.我做了一些挖掘,我没有想法。 Is there any way to make this simple script run on more than 1 thread?有没有办法让这个简单的脚本在 1 个以上的线程上运行? Will it speed up the process?它会加快这个过程吗?

Thanks!谢谢!

It would save you a lot of time if you first converted passes into a dictionary of some sort.如果您首先将传递转换为某种字典,它将为您节省大量时间。 I don't completely understand your code, but it seems that each user and each pass has some sort of id or key embedded in it, and you're looking to find matching ids.我不完全理解您的代码,但似乎每个user和每个pass都嵌入了某种 ID 或密钥,您正在寻找匹配的 ID。

Convert passes into a dictionary将通行证转换为字典

my_dictionary = { pass[:pass.find(':')] : pass for pass in passes } 

Your outer loop is then:那么你的外循环是:

for user in users:
    id = user[user.find(':') + 1:]
    if id in my_dictionary:
        pass = my_dictionary[id]
        .... whatever you do with user and pass ...
        del my_dictionary[id]

A single dictionary lookup instead of a nested loop.单个字典查找而不是嵌套循环。

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

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