Skip to content

Commit

Permalink
Use uvloop for asyncio
Browse files Browse the repository at this point in the history
This replacement loop is significantly faster for sockets and streams
  • Loading branch information
JHolba committed Nov 13, 2024
1 parent d673a07 commit d11ba38
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ dependencies = [
"tabulate",
"tqdm>=4.62.0",
"typing_extensions>=4.5",
"uvloop",
"uvicorn >= 0.17.0",
"websockets < 14",
"xarray",
Expand Down
9 changes: 6 additions & 3 deletions src/_ert/async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import logging
import traceback
from contextlib import suppress
from typing import Any, Coroutine, Generator, TypeVar, Union
from typing import Any, Coroutine, Generator, Mapping, TypeVar, Union

import uvloop

logger = logging.getLogger(__name__)

Expand All @@ -13,7 +15,7 @@


def new_event_loop() -> asyncio.AbstractEventLoop:
loop = asyncio.new_event_loop()
loop = uvloop.new_event_loop()
loop.set_task_factory(_create_task)
return loop

Expand All @@ -30,9 +32,10 @@ def get_running_loop() -> asyncio.AbstractEventLoop:
def _create_task(
loop: asyncio.AbstractEventLoop,
coro: Union[Coroutine[Any, Any, _T], Generator[Any, None, _T]],
**kwargs: Mapping[str, Any],
) -> asyncio.Task[_T]:
assert asyncio.iscoroutine(coro)
task = asyncio.Task(coro, loop=loop)
task = asyncio.Task(coro, loop=loop, **kwargs) # type: ignore
task.add_done_callback(_done_callback)
return task

Expand Down
2 changes: 2 additions & 0 deletions src/ert/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import Any, Dict, Optional, Sequence, Union
from uuid import UUID

import uvloop
import yaml
from opentelemetry.instrumentation.threading import ThreadingInstrumentor
from opentelemetry.trace import Status, StatusCode
Expand Down Expand Up @@ -651,6 +652,7 @@ def log_process_usage() -> None:

@tracer.start_as_current_span("ert.application.start")
def main() -> None:
uvloop.install()
span = trace.get_current_span()
warnings.filterwarnings("ignore", category=DeprecationWarning)
locale.setlocale(locale.LC_NUMERIC, "C")
Expand Down

0 comments on commit d11ba38

Please sign in to comment.