Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ repos:
- platformdirs>=2
- pytokens>=0.3.0
- tomli>=1.1.0,<2.0.0
- uvloop>=0.15.2; sys_platform != 'win32'
- winloop>=0.5.0; sys_platform == 'win32'

# blackd
- aiohttp>=3.10
Expand Down
10 changes: 8 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,21 @@

### Performance

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

### Output

<!-- Changes to Black's terminal output and error messages -->

### _Blackd_

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

### Integrations

Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ dynamic = ["readme", "version"]

[project.optional-dependencies]
colorama = ["colorama>=0.4.3"]
uvloop = ["uvloop>=0.15.2"]
uvloop = [
"uvloop>=0.15.2; sys_platform != 'win32'",
"winloop>=0.5.0; sys_platform == 'win32'"
]
d = ["aiohttp>=3.10"]
jupyter = ["ipython>=7.8.0", "tokenize-rt>=3.2.0"]

Expand Down
19 changes: 12 additions & 7 deletions src/black/concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,24 @@
from black.report import Changed, Report


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

uvloop.install()
return uvloop.new_event_loop()
else:
import winloop

return winloop.new_event_loop()
except ImportError:
pass
return asyncio.new_event_loop()


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

if workers is None:
workers = int(os.environ.get("BLACK_NUM_WORKERS", 0))
Expand Down Expand Up @@ -110,7 +115,7 @@ def reformat_many(
if executor is None:
executor = ThreadPoolExecutor(max_workers=1)

loop = asyncio.new_event_loop()
loop = maybe_use_uvloop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(
Expand Down
12 changes: 9 additions & 3 deletions src/blackd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import black
from _black_version import version as __version__
from black.concurrency import maybe_install_uvloop
from black.concurrency import maybe_use_uvloop

# This is used internally by tests to shut down the server prematurely
_stop_signal = asyncio.Event()
Expand Down Expand Up @@ -82,7 +82,14 @@ def main(bind_host: str, bind_port: int) -> None:
app = make_app()
ver = black.__version__
black.out(f"blackd version {ver} listening on {bind_host} port {bind_port}")
web.run_app(app, host=bind_host, port=bind_port, handle_signals=True, print=None)
web.run_app(
app,
host=bind_host,
port=bind_port,
handle_signals=True,
print=None,
loop=maybe_use_uvloop(),
)


@cache
Expand Down Expand Up @@ -245,7 +252,6 @@ def parse_python_variant_header(value: str) -> tuple[bool, set[black.TargetVersi


def patched_main() -> None:
maybe_install_uvloop()
freeze_support()
main()

Expand Down
Loading