From 11297debfe504c1d4ca018311ee1e08b6c4218f6 Mon Sep 17 00:00:00 2001 From: silvered-shark Date: Sat, 21 May 2022 09:26:30 +0800 Subject: [PATCH] feat(env): `NO_UVLOOP` to force disable uvloop https://github.com/MagicStack/uvloop/issues/471 https://github.com/aio-libs/aiohttp/issues/6762 Signed-off-by: Rongrong --- .env.sample | 1 + docker-compose.yml.sample | 1 + docs/advanced-settings.md | 5 +++-- src/__init__.py | 14 ++++---------- src/env.py | 11 +++++++++++ 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/.env.sample b/.env.sample index 15a3c8f..3c418f4 100644 --- a/.env.sample +++ b/.env.sample @@ -34,5 +34,6 @@ TELEGRAPH_TOKEN=" #TRAFFIC_SAVING=1 # default: 0 #LAZY_MEDIA_VALIDATION=1 # default: 0 #MANAGER_PRIVILEGED=1 # default: 0 +#NO_UVLOOP=1 # default: 0 #DEBUG=1 # debug logging, default: 0 # ↑------ Advanced settings ------↑ # diff --git a/docker-compose.yml.sample b/docker-compose.yml.sample index c45db0b..85570d4 100644 --- a/docker-compose.yml.sample +++ b/docker-compose.yml.sample @@ -44,5 +44,6 @@ services: #- TRAFFIC_SAVING=1 # default: 0 #- LAZY_MEDIA_VALIDATION=1 # default: 0 #- MANAGER_PRIVILEGED=1 # default: 0 + #- NO_UVLOOP=1 # default: 0 #- DEBUG=1 # debug logging, default: 0 # ↑------ Advanced settings ------↑ # \ No newline at end of file diff --git a/docs/advanced-settings.md b/docs/advanced-settings.md index 6218dc3..2f6447a 100644 --- a/docs/advanced-settings.md +++ b/docs/advanced-settings.md @@ -47,9 +47,10 @@ | `IMG_RELAY_SERVER` | Media relay server URL | `https://images.weserv.nl/?url=` | `https://rsstt-img-relay.rongrong.workers.dev/` | | `IMAGES_WESERV_NL` | images.weserv.nl URL | `https://t0.nl/` | `https://images.weserv.nl/` | | `DATABASE_URL` | Database URL [^7] | `postgres://user:pass@example.com:5432/table` | `sqlite://$PATH_TO_CONFIG/db.sqlite3?journal_mode=OFF` | -| `TABLE_TO_IMAGE` | Convert tables to image (causing high CPU usage) or just drop them? | `1` | `0` | -| `DEBUG` | Enable debug logging or not? | `1` | `0` | +| `TABLE_TO_IMAGE` | Convert tables to image (causing higher CPU load) or just drop them? | `1` | `0` | | `MANAGER_PRIVILEGED` | Allow the bot manager to manipulate any users' subscriptions or not? [^8] | `1` | `0` | +| `NO_UVLOOP` | Never enable `uvloop` (even if it is found) or not? | `1` | `0` | +| `DEBUG` | Enable debug logging or not? | `1` | `0` | ## Manager options diff --git a/src/__init__.py b/src/__init__.py index ffaff10..366d7df 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,14 +1,8 @@ from __future__ import annotations -import asyncio - -try: - import uvloop - - uvloop.install() -except ImportError: # uvloop does not support Windows - uvloop = None +from . import env # the event loop is initialized in env, so import it first +import asyncio from functools import partial from time import sleep from typing import Optional @@ -20,7 +14,7 @@ from random import sample from os import path -from . import env, log, db, command +from . import log, db, command from .i18n import i18n, ALL_LANGUAGES, get_commands_list from .parsing import tgraph @@ -249,7 +243,7 @@ def main(): f"R_PROXY (for RSS): {env.REQUESTS_PROXIES['all'] if env.REQUESTS_PROXIES else 'not set'}\n" f"DATABASE: {env.DATABASE_URL.split('://', 1)[0]}\n" f"TELEGRAPH: {f'Enable ({tgraph.apis.count} accounts)' if tgraph.apis else 'Disable'}\n" - f"UVLOOP: {'Enable' if uvloop is not None else 'Disable'}\n" + f"UVLOOP: {'Enable' if env.uvloop_enabled else 'Disable'}\n" f"MULTIUSER: {'Enable' if env.MULTIUSER else 'Disable'}") if env.MANAGER_PRIVILEGED: logger.warning('Bot manager privileged mode is enabled! ' diff --git a/src/env.py b/src/env.py index a412494..8ac0a0e 100644 --- a/src/env.py +++ b/src/env.py @@ -241,6 +241,7 @@ def __list_parser(var: Optional[str]) -> list[str]: TABLE_TO_IMAGE: Final = __bool_parser(os.environ.get('TABLE_TO_IMAGE')) TRAFFIC_SAVING: Final = __bool_parser(os.environ.get('TRAFFIC_SAVING')) LAZY_MEDIA_VALIDATION: Final = __bool_parser(os.environ.get('LAZY_MEDIA_VALIDATION')) +NO_UVLOOP: Final = __bool_parser(os.environ.get('NO_UVLOOP')) DEBUG: Final = __bool_parser(os.environ.get('DEBUG')) __configure_logging( # config twice to make .env file work level=colorlog.DEBUG if DEBUG else colorlog.INFO, @@ -286,5 +287,15 @@ def __list_parser(var: Optional[str]) -> list[str]: bot_peer: Optional[User] = None # placeholder bot_input_peer: Optional[InputPeerUser] = None # placeholder +# ----- loop initialization ----- +uvloop_enabled = False +if not NO_UVLOOP: + try: + import uvloop + + uvloop.install() + uvloop_enabled = True + except ImportError: # not installed (e.g. Windows) + uvloop = None loop = asyncio.new_event_loop() asyncio.set_event_loop(loop)