Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file lock to support mutli processes usage #614

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ mock = "*"
requests = "*"
python-dotenv = "*"
packaging = "*"
filelock = "*"
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
install_requires=[
'requests',
'python-dotenv',
'packaging'
'packaging',
'filelock'
],
package_data={
"webdriver_manager": ["py.typed"]
Expand Down
22 changes: 22 additions & 0 deletions tests/test_multi_processes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from concurrent.futures import ProcessPoolExecutor
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager


def _run(_):
chromedriver_path = ChromeDriverManager().install()
chrome_options = Options()
chrome_options.add_argument('--headless=new')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(service=Service(chromedriver_path), options=chrome_options)
url = 'https://www.google.com'
driver.set_page_load_timeout(10)
driver.get(url)
driver.quit()


def test_install_driver_in_multi_processes(delete_drivers_dir):
pool = ProcessPoolExecutor(max_workers=5)
assert list(pool.map(_run, range(5)))
19 changes: 12 additions & 7 deletions webdriver_manager/core/manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
from filelock import FileLock
from webdriver_manager.core.download_manager import WDMDownloadManager
from webdriver_manager.core.driver_cache import DriverCacheManager
from webdriver_manager.core.logger import log
from webdriver_manager.core.os_manager import OperationSystemManager
from webdriver_manager.core.constants import DEFAULT_USER_HOME_CACHE_PATH


class DriverManager(object):
Expand Down Expand Up @@ -32,14 +35,16 @@ def install(self) -> str:
raise NotImplementedError("Please Implement this method")

def _get_driver_binary_path(self, driver):
binary_path = self._cache_manager.find_driver(driver)
if binary_path:
file_lock = FileLock(os.path.join(DEFAULT_USER_HOME_CACHE_PATH, "drivers.json.lock"))
with file_lock:
binary_path = self._cache_manager.find_driver(driver)
if binary_path:
return binary_path

os_type = self.get_os_type()
file = self._download_manager.download_file(driver.get_driver_download_url(os_type))
binary_path = self._cache_manager.save_file_to_cache(driver, file)
return binary_path

os_type = self.get_os_type()
file = self._download_manager.download_file(driver.get_driver_download_url(os_type))
binary_path = self._cache_manager.save_file_to_cache(driver, file)
return binary_path

def get_os_type(self):
return self._os_system_manager.get_os_type()
Loading