Python多处理-冻结支持()错误
嗨,我正在尝试使用Selenium运行多个google chrome窗口,我需要能够使用函数启动子进程 这是我的代码:Python多处理-冻结支持()错误,python,selenium,multiprocessing,Python,Selenium,Multiprocessing,嗨,我正在尝试使用Selenium运行多个google chrome窗口,我需要能够使用函数启动子进程 这是我的代码: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as expect from selenium.webdriver.support.ui import WebD
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as expect
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import sys
from multiprocessing import Process, Value
PATH = "C:\chromedriver\chromedriver.exe"
def session(state):
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(PATH, options=options)
url = "https://www.google.com"
driver.get(url)
while True:
pass
sessions = []
def runSessions():
state = Value('i', 1)
for i in range(2):
p = Process(target=session, args=(state,))
sessions.append(p)
for p in sessions:
p.start()
runSessions()
但我甚至无法运行它,因为每当我调用函数runSessions()
,它都会抛出以下错误:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
两个变化:
- 在Windows中使用多处理模块时,需要检查main并调用冻结支持()
- 仅使用单个web驱动程序实例
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as expect
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import sys
from multiprocessing import Process, Value
PATH = "C:\chromedriver\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(PATH, options=options)
qlst = ['python','java'] # query for each instance
def session(state, idx):
url = "https://www.google.com/search?q=" + qlst[idx]
driver.get(url)
while True:
pass
sessions = []
def runSessions():
state = Value('i', 1)
for i in range(2):
p = Process(target=session, args=(state,i))
sessions.append(p)
for p in sessions:
p.start()
from multiprocessing import Process, freeze_support
if __name__ == '__main__':
freeze_support() # needed for Windows
runSessions()
如我在问题中所述,
我需要能够使用函数启动子进程。
这样就不会获得任何真正的并发性。换成Pypetteer。