0
for line in 'textfile.txt':
   myvariables = line.split(':')
   driver = ...
   driver.get("google.com")
   driver.quit()

Hello I am currently working with a simple script as the couple lines above... Here, per line in my text file, a selenium driver would open, get google, and quit. However, If there are, for example, two lines in my text file, one driver would open, then the second line would be handled after the first driver has quit. Is it possible to iterate over every line at the same time? So, basically, if there were two lines how could I make this run twice at the same time. Keeping this in one python file is a key factor for me. Appreciate any comments!

Bussarin
  • 11
  • 5
  • So what you want is an **a**synchronous loop? Have you considered threads? – Some programmer dude Mar 23 '21 at 08:18
  • 1
    There is literally an ``async for`` loop, but judging by your description that is not what you actually want. Your requirement seems more like a thread/process pool ``map``. – MisterMiyagi Mar 23 '21 at 08:34
  • I believe I meant asynchronus...They kind of act the opposite way haha (async and a loop), was figuring out the best way to ask this simply. Appreciate the help! Threading is easily a solution for this. – Bussarin Mar 23 '21 at 08:36
  • 1
    Does this answer your question? [Is there a simple process-based parallel map for python?](https://stackoverflow.com/questions/1704401/is-there-a-simple-process-based-parallel-map-for-python) – MisterMiyagi Mar 23 '21 at 08:36

1 Answers1

0
import selenium
import time
import random
import threading

def mydefini(self):
    while(True):
        driver = ....     
        driver.get("https://google.com")
        driver.get("https://youtube.com")
        driver.get("https://google.com")
            


with open("mytextfile.txt", "r") as a_file:
  for line in a_file:
      browserThread = threading.Thread(target=mydefini, args=("1"))    #args = "1" placed as a random positional argument to start the thread no problem (definitely a better solution to the random argument)
      browserThread.start()

copy and paste this should get you going with threading/selenium... if I have 3 lines in my text file 3 tasks will launch, grab google, youtube, then google again all at the same time... (if you copy and paste, note this is a loop, it will relaunch 3 tasks over and over again)

Bussarin
  • 11
  • 5