Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Add async_run function instead of set_event_loop_policy
Browse files Browse the repository at this point in the history
  • Loading branch information
monosans committed May 31, 2024
1 parent 83e599f commit 39d10a9
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions nitro_generator_checker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import asyncio
import logging
import sys
from typing import TYPE_CHECKING, Dict
from typing import TYPE_CHECKING, Coroutine, Dict

import aiofiles
import rich.traceback
from aiohttp import ClientSession, DummyCookieJar, TCPConnector
from rich.console import Console
from rich.logging import RichHandler
from typing_extensions import Any
from typing_extensions import Any, TypeVar

from . import http
from .nitro_checker import NitroChecker
Expand All @@ -26,31 +26,33 @@
else:
import tomli as tomllib

T = TypeVar("T")

def set_event_loop_policy() -> None:

def async_run(main: Coroutine[Any, Any, T]) -> T:
if sys.implementation.name == "cpython":
if sys.platform in {"cygwin", "win32"}:
try:
import winloop # type: ignore[import-not-found] # noqa: PLC0415
except ImportError:
pass
else:
try:
policy = winloop.EventLoopPolicy()
except AttributeError:
policy = winloop.WinLoopPolicy()
asyncio.set_event_loop_policy(policy)
return
elif sys.platform in {"darwin", "linux"}:
try:
import uvloop # noqa: PLC0415
except ImportError:
pass
else:
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
return
try:
import uvloop # type: ignore[import-not-found, unused-ignore] # noqa: PLC0415
except ImportError:
pass
else:
if hasattr(uvloop, "run"):
return uvloop.run(main) # type: ignore[no-any-return]
uvloop.install()
return asyncio.run(main)

try:
import winloop # type: ignore[import-not-found, import-untyped, unused-ignore] # noqa: PLC0415
except ImportError:
pass
else:
if hasattr(winloop, "run"):
return winloop.run(main) # type: ignore[no-any-return]
winloop.install()
return asyncio.run(main)
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
return asyncio.run(main)


async def read_config(file: str, /) -> Dict[str, Any]:
Expand Down Expand Up @@ -97,5 +99,4 @@ async def main() -> None:


if __name__ == "__main__":
set_event_loop_policy()
asyncio.run(main())
async_run(main())

0 comments on commit 39d10a9

Please sign in to comment.