繁体   English   中英

如何调整我的 Python 子进程代码,以便 python 脚本并行运行?

[英]How can I adjust my Python subprocess code so that the python scripts run in parallel?

我有两个 selenium python 代码。 我用子进程脚本运行它们:

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

当我运行它们时,它们不是并行运行,而是顺序运行。 有谁知道我如何调整这个脚本以便它们同时(并行)运行?

以下是脚本 1 和 2 的代码:

脚本1.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()

脚本2.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()

我不确定subprocess.run究竟是如何工作的,但我已经尝试过subprocess.Popen并且它运行良好。

尝试这个:

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()

暂无
暂无

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

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