Skip to content

Commit

Permalink
Merge pull request #64 from Krutyi-4el/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
solaluset authored Aug 14, 2023
2 parents dafc86a + 0dbcf00 commit e7ae90c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
12 changes: 9 additions & 3 deletions musicbot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from discord.ext import commands, bridge

from config import config
from musicbot import loader
from musicbot.bot import MusicBot
from musicbot.utils import check_dependencies, ShutdownReader
from musicbot.utils import check_dependencies, read_shutdown

del bridge

Expand Down Expand Up @@ -43,15 +44,20 @@


if __name__ == "__main__":
if "--run" in sys.argv:
ShutdownReader().start()
print("Loading...")

check_dependencies()
config.warn_unknown_vars()
config.save()

bot.load_extensions(*initial_extensions)

# start executor before reading from stdin to avoid deadlocks
loader.init()

if "--run" in sys.argv:
shutdown_task = bot.loop.create_task(read_shutdown())

try:
bot.run(config.BOT_TOKEN, reconnect=True)
except discord.LoginFailure:
Expand Down
25 changes: 24 additions & 1 deletion musicbot/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,36 @@
sys.stdout = OutputWrapper(sys.stdout)
sys.stderr = OutputWrapper(sys.stderr)

_context = mp_context("spawn")


class LoaderProcess(_context.Process):
def run(self):
try:
super().run()
# suppress noisy errors that happen on Ctrl+C
except (KeyboardInterrupt, InterruptedError):
pass


_context.Process = LoaderProcess

_loop = asyncio.new_event_loop()
_executor = ProcessPoolExecutor(1, mp_context("spawn"))
_executor = ProcessPoolExecutor(1, _context)
_cached_downloaders: List[Tuple[dict, yt_dlp.YoutubeDL]] = []
_preloading = {}
_search_lock = threading.Lock()


def _noop():
pass


def init():
# wake it up to spawn the process immediately
_executor.submit(_noop).result()


def extract_info(url: str, options: dict) -> dict:
downloader = None
for o, d in _cached_downloaders:
Expand Down
22 changes: 9 additions & 13 deletions musicbot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import _thread
import asyncio
from enum import Enum
from threading import Thread
from aioconsole import ainput
from subprocess import DEVNULL, check_call
from typing import TYPE_CHECKING, Callable, Awaitable, Optional, Union

Expand All @@ -26,7 +26,7 @@


def check_dependencies():
assert pycord_version == "2.5.2", (
assert pycord_version == "2.5.3", (
"you don't have necessary version of Pycord."
" Please install the version specified in requirements.txt"
)
Expand Down Expand Up @@ -230,14 +230,10 @@ def get_log_file(cls):
return cls.log_file


class ShutdownReader(Thread):
def __init__(self):
super().__init__(name=type(self).__name__)

def run(self):
try:
line = input()
except EOFError:
return
if line == "shutdown":
_thread.interrupt_main()
async def read_shutdown():
try:
line = await ainput()
except EOFError:
return
if line == "shutdown":
_thread.interrupt_main()
13 changes: 7 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
py-cord[voice] @ https://github.com/Krutyi-4el/pycord/archive/bd32b8d66579abd51daa381c26df59e449067df5.zip
yt-dlp==2023.6.21
aiohttp==3.8.4
py-cord[voice] @ https://github.com/Krutyi-4el/pycord/archive/0d5816021403a667a330a5b7f4286348b2c21bf9.zip
yt-dlp==2023.7.6
aiohttp==3.8.5
beautifulsoup4==4.12.2
spotipy==2.23.0
emoji==2.2.0
SQLAlchemy[asyncio]==2.0.16
alembic==1.11.1
emoji==2.7.0
SQLAlchemy[asyncio]==2.0.19
alembic==1.11.2
aioconsole==0.6.2
./config

0 comments on commit e7ae90c

Please sign in to comment.