Skip to content

Commit 11297de

Browse files
feat(env): NO_UVLOOP to force disable uvloop
MagicStack/uvloop#471 aio-libs/aiohttp#6762 Signed-off-by: Rongrong <[email protected]>
1 parent 0280335 commit 11297de

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

.env.sample

+1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ TELEGRAPH_TOKEN="
3434
#TRAFFIC_SAVING=1 # default: 0
3535
#LAZY_MEDIA_VALIDATION=1 # default: 0
3636
#MANAGER_PRIVILEGED=1 # default: 0
37+
#NO_UVLOOP=1 # default: 0
3738
#DEBUG=1 # debug logging, default: 0
3839
# ↑------ Advanced settings ------↑ #

docker-compose.yml.sample

+1
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ services:
4444
#- TRAFFIC_SAVING=1 # default: 0
4545
#- LAZY_MEDIA_VALIDATION=1 # default: 0
4646
#- MANAGER_PRIVILEGED=1 # default: 0
47+
#- NO_UVLOOP=1 # default: 0
4748
#- DEBUG=1 # debug logging, default: 0
4849
# ↑------ Advanced settings ------↑ #

docs/advanced-settings.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@
4747
| `IMG_RELAY_SERVER` | Media relay server URL | `https://images.weserv.nl/?url=` | `https://rsstt-img-relay.rongrong.workers.dev/` |
4848
| `IMAGES_WESERV_NL` | images.weserv.nl URL | `https://t0.nl/` | `https://images.weserv.nl/` |
4949
| `DATABASE_URL` | Database URL [^7] | `postgres://user:[email protected]:5432/table` | `sqlite://$PATH_TO_CONFIG/db.sqlite3?journal_mode=OFF` |
50-
| `TABLE_TO_IMAGE` | Convert tables to image (causing high CPU usage) or just drop them? | `1` | `0` |
51-
| `DEBUG` | Enable debug logging or not? | `1` | `0` |
50+
| `TABLE_TO_IMAGE` | Convert tables to image (causing higher CPU load) or just drop them? | `1` | `0` |
5251
| `MANAGER_PRIVILEGED` | Allow the bot manager to manipulate any users' subscriptions or not? [^8] | `1` | `0` |
52+
| `NO_UVLOOP` | Never enable `uvloop` (even if it is found) or not? | `1` | `0` |
53+
| `DEBUG` | Enable debug logging or not? | `1` | `0` |
5354

5455
## Manager options
5556

src/__init__.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
from __future__ import annotations
22

3-
import asyncio
4-
5-
try:
6-
import uvloop
7-
8-
uvloop.install()
9-
except ImportError: # uvloop does not support Windows
10-
uvloop = None
3+
from . import env # the event loop is initialized in env, so import it first
114

5+
import asyncio
126
from functools import partial
137
from time import sleep
148
from typing import Optional
@@ -20,7 +14,7 @@
2014
from random import sample
2115
from os import path
2216

23-
from . import env, log, db, command
17+
from . import log, db, command
2418
from .i18n import i18n, ALL_LANGUAGES, get_commands_list
2519
from .parsing import tgraph
2620

@@ -249,7 +243,7 @@ def main():
249243
f"R_PROXY (for RSS): {env.REQUESTS_PROXIES['all'] if env.REQUESTS_PROXIES else 'not set'}\n"
250244
f"DATABASE: {env.DATABASE_URL.split('://', 1)[0]}\n"
251245
f"TELEGRAPH: {f'Enable ({tgraph.apis.count} accounts)' if tgraph.apis else 'Disable'}\n"
252-
f"UVLOOP: {'Enable' if uvloop is not None else 'Disable'}\n"
246+
f"UVLOOP: {'Enable' if env.uvloop_enabled else 'Disable'}\n"
253247
f"MULTIUSER: {'Enable' if env.MULTIUSER else 'Disable'}")
254248
if env.MANAGER_PRIVILEGED:
255249
logger.warning('Bot manager privileged mode is enabled! '

src/env.py

+11
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def __list_parser(var: Optional[str]) -> list[str]:
241241
TABLE_TO_IMAGE: Final = __bool_parser(os.environ.get('TABLE_TO_IMAGE'))
242242
TRAFFIC_SAVING: Final = __bool_parser(os.environ.get('TRAFFIC_SAVING'))
243243
LAZY_MEDIA_VALIDATION: Final = __bool_parser(os.environ.get('LAZY_MEDIA_VALIDATION'))
244+
NO_UVLOOP: Final = __bool_parser(os.environ.get('NO_UVLOOP'))
244245
DEBUG: Final = __bool_parser(os.environ.get('DEBUG'))
245246
__configure_logging( # config twice to make .env file work
246247
level=colorlog.DEBUG if DEBUG else colorlog.INFO,
@@ -286,5 +287,15 @@ def __list_parser(var: Optional[str]) -> list[str]:
286287
bot_peer: Optional[User] = None # placeholder
287288
bot_input_peer: Optional[InputPeerUser] = None # placeholder
288289

290+
# ----- loop initialization -----
291+
uvloop_enabled = False
292+
if not NO_UVLOOP:
293+
try:
294+
import uvloop
295+
296+
uvloop.install()
297+
uvloop_enabled = True
298+
except ImportError: # not installed (e.g. Windows)
299+
uvloop = None
289300
loop = asyncio.new_event_loop()
290301
asyncio.set_event_loop(loop)

0 commit comments

Comments
 (0)