Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b9d3e3

Browse files
Vizonexcobaltt7pre-commit-ci[bot]
authored
add winloop support and remove deprecated functionality from uvloop (psf#4996)
* add winloop support and remove deprecated functionality from uvloop * summarize changes made in pull request psf#4996 * close after running with web.run_app * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * reformat * add windows-latest and windows-11-arm to uvloop test --------- Co-authored-by: cobalt <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 457320a commit 8b9d3e3

6 files changed

Lines changed: 41 additions & 14 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ jobs:
105105
strategy:
106106
fail-fast: false
107107
matrix:
108-
os: [ubuntu-latest, macOS-latest]
108+
os: [ubuntu-latest, macOS-latest, windows-latest, windows-11-arm]
109109

110110
steps:
111111
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ repos:
5252
- platformdirs>=2
5353
- pytokens~=0.4.0
5454
- tomli>=1.1.0
55+
- uvloop>=0.15.2; sys_platform != 'win32'
56+
- winloop>=0.5.0; sys_platform == 'win32'
5557

5658
# blackd
5759
- aiohttp>=3.10

CHANGES.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@
4343

4444
### Performance
4545

46-
<!-- Changes that improve Black's performance. -->
46+
- Introduce winloop for windows as an alternative to uvloop (#4996)
47+
- Remove deprecated function `uvloop.install()` in favor of `uvloop.new_event_loop()`
48+
(#4996)
49+
- Rename `maybe_install_uvloop` function to `maybe_use_uvloop` to simplify loop
50+
installation and creation of either a uvloop/winloop evenloop or default eventloop
51+
(#4996)
4752

4853
### Output
4954

@@ -56,7 +61,8 @@
5661

5762
### _Blackd_
5863

59-
<!-- Changes to blackd -->
64+
- Introduce winloop to be used when windows in use which enables blackd to run faster on
65+
windows when winloop is installed. (#4996)
6066

6167
### Integrations
6268

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ dynamic = ["readme", "version"]
6666

6767
[project.optional-dependencies]
6868
colorama = ["colorama>=0.4.3"]
69-
uvloop = ["uvloop>=0.15.2"]
69+
uvloop = [
70+
"uvloop>=0.15.2; sys_platform != 'win32'",
71+
"winloop>=0.5.0; sys_platform == 'win32'"
72+
]
7073
d = ["aiohttp>=3.10"]
7174
jupyter = ["ipython>=7.8.0", "tokenize-rt>=3.2.0"]
7275

src/black/concurrency.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,24 @@
2727
from black.report import Changed, Report
2828

2929

30-
def maybe_install_uvloop() -> None:
31-
"""If our environment has uvloop installed we use it.
30+
def maybe_use_uvloop() -> asyncio.AbstractEventLoop:
31+
"""If our environment has uvloop or winloop installed we use it otherwise
32+
a normal asyncio eventloop is called as fallback.
3233
3334
This is called only from command-line entry points to avoid
3435
interfering with the parent process if Black is used as a library.
3536
"""
3637
try:
37-
import uvloop
38+
if sys.platform != "win32":
39+
import uvloop
3840

39-
uvloop.install()
41+
return uvloop.new_event_loop()
42+
else:
43+
import winloop
44+
45+
return winloop.new_event_loop()
4046
except ImportError:
41-
pass
47+
return asyncio.new_event_loop()
4248

4349

4450
def cancel(tasks: Iterable[asyncio.Future[Any]]) -> None:
@@ -81,7 +87,6 @@ def reformat_many(
8187
no_cache: bool = False,
8288
) -> None:
8389
"""Reformat multiple files using a ProcessPoolExecutor."""
84-
maybe_install_uvloop()
8590

8691
if workers is None:
8792
workers = int(os.environ.get("BLACK_NUM_WORKERS", 0))
@@ -110,7 +115,7 @@ def reformat_many(
110115
if executor is None:
111116
executor = ThreadPoolExecutor(max_workers=1)
112117

113-
loop = asyncio.new_event_loop()
118+
loop = maybe_use_uvloop()
114119
asyncio.set_event_loop(loop)
115120
try:
116121
loop.run_until_complete(

src/blackd/__init__.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import black
2323
from _black_version import version as __version__
24-
from black.concurrency import maybe_install_uvloop
24+
from black.concurrency import maybe_use_uvloop
2525

2626
# This is used internally by tests to shut down the server prematurely
2727
_stop_signal = asyncio.Event()
@@ -82,7 +82,19 @@ def main(bind_host: str, bind_port: int) -> None:
8282
app = make_app()
8383
ver = black.__version__
8484
black.out(f"blackd version {ver} listening on {bind_host} port {bind_port}")
85-
web.run_app(app, host=bind_host, port=bind_port, handle_signals=True, print=None)
85+
loop = maybe_use_uvloop()
86+
try:
87+
web.run_app(
88+
app,
89+
host=bind_host,
90+
port=bind_port,
91+
handle_signals=True,
92+
print=None,
93+
loop=loop,
94+
)
95+
finally:
96+
if not loop.is_closed():
97+
loop.close()
8698

8799

88100
@cache
@@ -245,7 +257,6 @@ def parse_python_variant_header(value: str) -> tuple[bool, set[black.TargetVersi
245257

246258

247259
def patched_main() -> None:
248-
maybe_install_uvloop()
249260
freeze_support()
250261
main()
251262

0 commit comments

Comments
 (0)