简体   繁体   English

从一个文件中读取同一行的线程

[英]Threads reading the same line from one file

I'm trying to use threads with python, I'm pretty new to threads.我正在尝试将线程与 python 一起使用,我对线程很陌生。 I wanted the threads to read random lines from the same file but all the threads read the same line.我希望线程从同一个文件中读取随机行,但所有线程都读取同一行。 So the file I'm trying to read has all the lines in email:pass:another_line format.所以我试图读取的文件包含 email:pass:another_line 格式的所有行。 I expected to read different lines from the same file with multiple threads but its reading the same line with multiple threads.我希望用多个线程从同一个文件中读取不同的行,但它用多个线程读取同一行。 So for example 1 thread will return line1, second thread will return line2 but its random lines.因此,例如 1 个线程将返回 line1,第二个线程将返回 line2 但它的随机行。

import random
import threading

def email_pass_token():
    global email, pass2, token

    file = open("testing/pokens.csv").read().splitlines()
    acc_str = random.choice(file)

    num_lines = sum(1 for _ in file)
    print(num_lines)

    email = ":".join(acc_str.split(":", 1)[:1])

    pass2 = ":".join(acc_str.split(":", 2)[:2][1:])

    token = ":".join(acc_str.split(":", 3)[:3][2:])
email_pass_token()

def gen_acc():
    print(email, pass2, token)

threads = []
num_thread = input("Threads: ")
num_thread = int(num_thread)
for i in range(num_thread):
    t = threading.Thread(target=gen_acc)
    threads.append(t)
    t.start()

File Sample:文件样本:

tqqe435ihwbta@oncemail.co.kr:#354946345e696$e30*417:another_line1
tkbzw543vwxzn@oncemail.co.kr:2e5548c543709!8@305-8(:another_line2
jcgeau43yphr@oncemail.co.kr:41c!954=7543cc^1#48fd_$*b5:another_line3
bqajs543qlqys@oncemail.co.kr:1f@e54d78^feb54355&6$50:another_line4
tqqeihw54bta@oncemail.co.kr:#3946345e696$e30*417:another_line5
tkbzwvwx543zn@oncemail.co.kr:2e58c5437709!8@305-8(:another_line6
jcgeauy453phr@oncemail.co.kr:41c!9=7543cc^1#48fd_$*b5:another_line7
bqajsqlq54ys@oncemail.co.kr:1f@ed78^feb53455&6$50:another_line8

You are not running the function for selecting line in the threads.您没有运行 function 来选择线程中的行。 What you are doing is you are running gen_acc in the threads which only prints the three variables email , pass2 and token .你正在做的是你在只打印三个变量emailpass2token的线程中运行gen_acc Your code for selecting lines resides email_pass_token which is called only once.您用于选择行的代码驻留在email_pass_token中,它只被调用一次。

Here's one way you could do it.这是您可以做到的一种方法。 Note that this will not work if the password contains a colon.请注意,如果密码包含冒号,这将不起作用 Also, I've hard-coded the number of threads but you get the idea.另外,我已经硬编码了线程数,但你明白了。

import random
from concurrent.futures import ThreadPoolExecutor

FILENAME = 'testing/pokens.csv'
NTHREADS = 5

def myfunc():
    with open(FILENAME) as infile:
        lines = infile.readlines()
        line = random.choice(lines).strip()
        tokens = line.split(':')
        return ' '.join(tokens)

def main():
    with ThreadPoolExecutor() as executor:
        futures = []
        printable = set()
        for _ in range(NTHREADS):
            future = executor.submit(myfunc)
            futures.append(future)
        for future in futures:
            printable.add(future.result())
        for p in printable:
            print(p)

if __name__ == '__main__':
    main()

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

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