Iterating through each line of multiple text files, unique to each thread in Python selenium

China☆狼群 提交于 2021-02-11 16:39:28

问题


So this code below does half the job I'm looking for, but I would like to implement a second text file along with the first one that basically does the same for another variable. So the first thread will read the first line of text file 1 and also the first line of text file 2, then the second thread will read the second line of text file 1 and the second line of text file 2.

Sounds confusing, buts its not.

Thanks in advance!!

import threading
import time

    def test_logic(file, file2): # ***Changed this function***
    
        line = myfile.readline()
        if line != '':
            user, password =  line.split(':')

        line2 = myfile2.readline()
    
    N = 5   # Number of browsers to spawn
    thread_list = list()
    myfile = open("prox.txt", 'r') # ***Opened file at the begining***
    myfile2 = open("prox2.txt", 'r') # ***Opened file at the begining***
    # Start test
    for i in range(N):
        t = threading.Thread(args=(myfile, myfile2), name='Test {}'.format(i), target=test_logic) # ***Passed file as an argument***
        t.start()
        time.sleep(1)
        print ("t.name +  started!")
    
        thread_list.append(t)
    
    # Wait for all thre<ads to complete
    for thread in thread_list:
        thread.join()
    
    print("Test completed!")

EDIT: Now Working with the edit, it was as simple as adding the second text file to the args of the thread. Hope this helps someone else.

来源:https://stackoverflow.com/questions/65386391/iterating-through-each-line-of-multiple-text-files-unique-to-each-thread-in-pyt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!