Skip to content

Commit 6f721b8

Browse files
authored
drop dependency on 'urllib3' (#9273)
1 parent 5f85df2 commit 6f721b8

7 files changed

Lines changed: 26 additions & 31 deletions

File tree

.pre-commit-config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ repos:
3838
- pytest
3939
- tornado
4040
- pyarrow
41-
- urllib3
4241
- git+https://github.com/dask/dask
4342
- git+https://github.com/dask/zict
4443

continuous_integration/environment-mindeps.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ dependencies:
1919
- tblib=1.6.0
2020
- toolz=0.12.0
2121
- tornado=6.2.0
22-
- urllib3=1.26.5
2322
- zict=3.0.0
2423
# Distributed depends on the latest version of Dask
2524
- pip

continuous_integration/recipes/distributed/meta.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ requirements:
4141
- tblib >=1.6.0,!=3.2.0,!=3.2.1
4242
- toolz >=0.10.0
4343
- tornado >=6.2.0
44-
- urllib3 >=1.26.5
4544
- zict >=3.0.0
4645
run_constrained:
4746
- openssl !=1.1.1e

distributed/preloading.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import os
77
import shutil
88
import sys
9+
import time
10+
import urllib.error
11+
import urllib.request
912
from collections.abc import Iterable, Sequence
1013
from importlib import import_module
1114
from types import ModuleType
@@ -129,21 +132,22 @@ def _import_module(name: str, file_dir: str | None = None) -> ModuleType:
129132
def _download_module(url: str) -> ModuleType:
130133
logger.info("Downloading preload at %s", url)
131134
assert is_webaddress(url)
132-
# This is the only place where urllib3 is used and it is a relatively heavy
133-
# import. Do lazy import to reduce import time
134-
import urllib3
135-
136-
with urllib3.PoolManager() as http:
137-
response = http.request(
138-
method="GET",
139-
url=url,
140-
retries=urllib3.util.Retry(
141-
status_forcelist=[429, 504, 503, 502],
142-
backoff_factor=0.2,
143-
),
144-
)
145135

146-
source = response.data
136+
retryable_codes = {429, 502, 503, 504}
137+
backoff_factor = 0.2
138+
max_retries = 3
139+
140+
for attempt in range(max_retries + 1):
141+
try:
142+
with urllib.request.urlopen(url) as response:
143+
source = response.read()
144+
break
145+
except urllib.error.HTTPError as e:
146+
if e.code in retryable_codes and attempt < max_retries:
147+
retry_delay_seconds = backoff_factor * (2**attempt)
148+
time.sleep(retry_delay_seconds)
149+
continue
150+
raise
147151

148152
compiled = compile(source, url, "exec")
149153
module = ModuleType(url)

distributed/tests/test_preload.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,13 @@ async def test_preload_import_time():
176176
async def test_web_preload():
177177
with (
178178
mock.patch(
179-
"urllib3.PoolManager.request",
179+
"urllib.request.urlopen",
180180
**{
181-
"return_value.data": b"def dask_setup(dask_server):"
181+
"return_value.__enter__.return_value.read.return_value": b"def dask_setup(dask_server):"
182182
b"\n dask_server.foo = 1"
183183
b"\n"
184184
},
185-
) as request,
185+
) as mock_urlopen,
186186
captured_logger("distributed.preloading") as log,
187187
):
188188
async with Scheduler(
@@ -200,9 +200,7 @@ async def test_web_preload():
200200
)
201201
is not None
202202
)
203-
assert request.mock_calls == [
204-
mock.call(method="GET", url="http://example.com/preload", retries=mock.ANY)
205-
]
203+
assert mock_urlopen.call_args_list == [mock.call("http://example.com/preload")]
206204

207205

208206
@gen_cluster(nthreads=[])
@@ -233,15 +231,13 @@ async def test_web_preload_worker():
233231
dask.config.set(scheduler_address="tcp://127.0.0.1:{port}")
234232
""").encode()
235233
with mock.patch(
236-
"urllib3.PoolManager.request",
237-
**{"return_value.data": data},
238-
) as request:
234+
"urllib.request.urlopen",
235+
**{"return_value.__enter__.return_value.read.return_value": data},
236+
) as mock_urlopen:
239237
async with Scheduler(port=port, host="localhost", dashboard_address=":0") as s:
240238
async with Nanny(preload_nanny=["http://example.com/preload"]) as nanny:
241239
assert nanny.scheduler_addr == s.address
242-
assert request.mock_calls == [
243-
mock.call(method="GET", url="http://example.com/preload", retries=mock.ANY)
244-
]
240+
assert mock_urlopen.call_args_list == [mock.call("http://example.com/preload")]
245241

246242

247243
# This test is blocked on https://github.com/dask/distributed/issues/5819

pixi.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ psutil = ">=5.8.0"
3030
sortedcontainers = ">=2.0.5"
3131
tblib = ">=1.6.0,!=3.2.0,!=3.2.1"
3232
tornado = ">=6.2.0"
33-
urllib3 = ">=1.26.5"
3433
zict = ">=3.0.0"

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ dependencies = [
4040
"tblib >= 1.6.0,!=3.2.0,!=3.2.1",
4141
"toolz >= 0.12.0",
4242
"tornado >= 6.2.0",
43-
"urllib3 >= 1.26.5",
4443
"zict >= 3.0.0",
4544
]
4645
dynamic = ["version"]

0 commit comments

Comments
 (0)