How to get user input while Multiprocessing

Viewed 165

I am trying to run multiple selenium instances in which I need to enter captchas, but I am a beginner in multiprocessing.

So while running and its time to give input it shows an error:

EOFError: EOF when reading a line

Here is an example of the code I am running:

import time
from selenium import webdriver
import multiprocessing

def first():
    chromedriver = "C:\chromedriver"
    driver = webdriver.Chrome(chromedriver)
    driver.set_window_size(1000, 1000)
    driver.get('https://www.google.com/')
    time.sleep(5)
    captcha1 = input("in1: ")
    print(inn)

def sec():
    chromedriver = "C:\chromedriver"
    driverr = webdriver.Chrome(chromedriver)
    driverr.set_window_size(1000, 1000)
    driverr.get('https://www.google.com/')
    captcha2 = input("in2: ")
    print(ins)

if __name__ == '__main__':
    p1 = multiprocessing.Process(target=first)
    p2 = multiprocessing.Process(target=sec)
    p1.start()
    p2.start()

    p1.join()
    p2.join()

Not only do I need to know how to give input but in this instance the 'captcha2' input would be needed first, so the 'captcha1' would have to wait until 'captcha2' is given...

1 Answers

You need to send messages requesting user input back to the main process so that it (and only it) can ask the user about them. The simplest way to do this is probably to create a multiprocessing.Queue object for the requests (so that the main process can listen to all children) and a Pipe for each process for the answers. Each request would of course be labeled with an identifier for the process sending it so that the response could be sent to the right place.

Related