Skip to content

Commit 5fd88b1

Browse files
committed
Make improvements to multithreaded downloads
1 parent 9f1f745 commit 5fd88b1

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6427,11 +6427,15 @@ def save_element_as_image_file(
64276427
def download_file(self, file_url, destination_folder=None):
64286428
"""Downloads the file from the url to the destination folder.
64296429
If no destination folder is specified, the default one is used.
6430-
(The default [Downloads Folder] = "./downloaded_files")"""
6431-
if not destination_folder:
6432-
destination_folder = constants.Files.DOWNLOADS_FOLDER
6433-
if not os.path.exists(destination_folder):
6434-
os.makedirs(destination_folder)
6430+
(The default folder for downloads is "./downloaded_files")"""
6431+
download_file_lock = fasteners.InterProcessLock(
6432+
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
6433+
)
6434+
with download_file_lock:
6435+
if not destination_folder:
6436+
destination_folder = constants.Files.DOWNLOADS_FOLDER
6437+
if not os.path.exists(destination_folder):
6438+
os.makedirs(destination_folder)
64356439
page_utils._download_file_to(file_url, destination_folder)
64366440
if self.recorder_mode:
64376441
url = self.get_current_url()

seleniumbase/fixtures/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class MultiBrowser:
124124
DRIVER_FIXING_LOCK = Files.DOWNLOADS_FOLDER + "/driver_fixing.lock"
125125
DRIVER_REPAIRED = Files.DOWNLOADS_FOLDER + "/driver_fixed.lock"
126126
CERT_FIXING_LOCK = Files.DOWNLOADS_FOLDER + "/cert_fixing.lock"
127+
DOWNLOAD_FILE_LOCK = Files.DOWNLOADS_FOLDER + "/download_file.lock"
127128

128129

129130
class SavedCookies:

seleniumbase/fixtures/page_utils.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
"""
2-
This module contains useful utility methods.
3-
"""
1+
"""This module contains useful utility methods."""
42
import codecs
3+
import fasteners
54
import os
65
import re
76
import requests
7+
from seleniumbase.fixtures import constants
88

99

1010
def get_domain_url(url):
@@ -261,8 +261,12 @@ def _download_file_to(file_url, destination_folder, new_file_name=None):
261261
file_name = file_url.split("/")[-1]
262262
r = requests.get(file_url)
263263
file_path = os.path.join(destination_folder, file_name)
264-
with open(file_path, "wb") as code:
265-
code.write(r.content)
264+
download_file_lock = fasteners.InterProcessLock(
265+
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
266+
)
267+
with download_file_lock:
268+
with open(file_path, "wb") as code:
269+
code.write(r.content)
266270

267271

268272
def _save_data_as(data, destination_folder, file_name):

0 commit comments

Comments
 (0)