Skip to content

Commit

Permalink
feat(env): NO_UVLOOP to force disable uvloop
Browse files Browse the repository at this point in the history
MagicStack/uvloop#471
aio-libs/aiohttp#6762
Signed-off-by: Rongrong <i@rong.moe>
  • Loading branch information
Rongronggg9 committed May 21, 2022
1 parent 87d988b commit ec87b4a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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 ------↑ #
1 change: 1 addition & 0 deletions docker-compose.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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 ------↑ #
5 changes: 3 additions & 2 deletions docs/advanced-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 4 additions & 10 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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! '
Expand Down
11 changes: 11 additions & 0 deletions src/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

0 comments on commit ec87b4a

Please sign in to comment.