Home > Software engineering >  How can I adjust my Python subprocess code so that the python scripts run in parallel?
How can I adjust my Python subprocess code so that the python scripts run in parallel?

Time:04-26

I have two selenium python codes. I am running them with a subprocess script:

import subprocess
subprocess.run("python script1.py & python script2.py", cwd=r'C:\Users\David\Desktop\Selenium', shell=True)

When I run them, they don't run in parallel but rather they run sequentially. Does anyone know how I can adapt this script so that they run at the same time (in parallel)?

Here are the codes for script 1 & 2:

script1.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options=Options()

driver=webdriver.Chrome(options=options)

params={'behavior':'allow','downloadPath':r'C:\Users\David\Downloads'}
driver.execute_cdp_cmd('Page.setDownloadBehavior',params)

driver.get("https://data.gov.uk/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/main/div[2]/form/div/div/input"))).send_keys("Forestry Statistics 2018: Recreation")

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/main/div[2]/form/div/div/div/button"))).click()


WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/form/main/div/div[2]/div[2]/div[2]/h2/a"))).click()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/main/div/div/div/section/table/tbody/tr[2]/td[1]/a"))).click()

script2.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options=Options()

driver=webdriver.Chrome(options=options)

params={'behavior':'allow','downloadPath':r'C:\Users\David\Downloads'}
driver.execute_cdp_cmd('Page.setDownloadBehavior',params)

driver.get("https://data.gov.uk/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/main/div[2]/form/div/div/input"))).send_keys("NI 179 - Value for money")

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/main/div[2]/form/div/div/div/button"))).click()


WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/form/main/div/div[2]/div[2]/div[2]/h2/a"))).click()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/main/div/div/div/section/table/tbody/tr/td[1]/a"))).click()

CodePudding user response:

I am unsure how exactly subprocess.run works but I have tried with subprocess.Popen and it runs fine.

Try this:

import subprocess
firstScript = subprocess.Popen(["python", "C:/Users/David/Desktop/Selenium/script1.py"], shell=True)
secondScript = subprocess.Popen(["python", "C:/Users/David/Desktop/Selenium/script2.py"], shell=True)

firstScript.wait()
secondScript.wait()

CodePudding user response:

try to use pytest-xdist

pip install pytest-xdist

pytest -n auto

  • Related