Skip to content

Commit

Permalink
Merge pull request #33 from xgui3783/feat_improveHttpAccessor
Browse files Browse the repository at this point in the history
Improve read perf of http accessor by using requests.Session
  • Loading branch information
ylep authored Jul 21, 2023
2 parents b7b5047 + 47fd3b1 commit a6bce3f
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/neuroglancer_scripts/http_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class HttpAccessor(neuroglancer_scripts.accessor.Accessor):
can_write = False

def __init__(self, base_url):
self._session = requests.Session()

# Fix the base URL to end with a slash, discard query and fragment
r = urllib.parse.urlsplit(base_url)
self.base_url = urllib.parse.urlunsplit((
Expand All @@ -55,7 +57,7 @@ def fetch_chunk(self, key, chunk_coords):
def file_exists(self, relative_path):
file_url = self.base_url + relative_path
try:
r = requests.head(file_url)
r = self._session.head(file_url)
if r.status_code == requests.codes.not_found:
return False
r.raise_for_status()
Expand All @@ -67,7 +69,7 @@ def file_exists(self, relative_path):
def fetch_file(self, relative_path):
file_url = self.base_url + relative_path
try:
r = requests.get(file_url)
r = self._session.get(file_url)
r.raise_for_status()
except requests.exceptions.RequestException as exc:
raise DataAccessError("Error reading {0}: {1}"
Expand Down

0 comments on commit a6bce3f

Please sign in to comment.