-
I have code like this: class TestClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
logging.info("Client initialised")
if platform.system() != "Windows":
signals = (
signal.SIGHUP,
signal.SIGTERM,
signal.SIGINT,
signal.SIGQUIT,
)
logging.info("Not on Windows, registering signal handlers")
for s in signals:
self.loop.add_signal_handler(
s, lambda sig=s: self.loop.create_task(self.shutdown(sig=sig))
)
async def on_ready(self):
logging.info("Ready")
self.loop.create_task(asyncio.sleep(100))
async def shutdown(self, sig: signal.Signals = None) -> None:
logging.info("shutdown!")
if sig is not None:
logging.info(f"Shutdown due to signal: {sig.name}")
logging.info("Calling stop")
self.loop.stop()
c = TestClient()
c.run(token) And what I expect to happen when it received a SIGTERM is for the shutdown task to run and then for the script to stop. What actually happens is this: (The ready line is from my code in If I remove the discord.py code and treat this like a normal asyncio script, it works: class TestClient():
def __init__(self, *args, **kwargs):
logging.info("Client initialised")
self.loop = asyncio.get_event_loop()
if platform.system() != "Windows":
signals = (
signal.SIGHUP,
signal.SIGTERM,
signal.SIGINT,
signal.SIGQUIT,
)
logging.info("Not on Windows, registering signal handlers")
for s in signals:
self.loop.add_signal_handler(
s, lambda sig=s: self.loop.create_task(self.shutdown(sig=sig))
)
self.loop.create_task(asyncio.sleep(100))
async def shutdown(self, sig: signal.Signals = None) -> None:
logging.info("shutdown!")
if sig is not None:
logging.info(f"Shutdown due to signal: {sig.name}")
logging.info("Calling stop")
self.loop.stop()
TestClient() Now when I send a SIGTERM shutdown is actually run: My question is how can I get my discord.py class to behave the same as the second example? It seems like there's some internal code in discord.py that is essentially killing my shutdown Task before it gets a chance to run? Edit: I just realized this may have something to do with me using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
In case anyone was wondering, it was because I was using |
Beta Was this translation helpful? Give feedback.
In case anyone was wondering, it was because I was using
client.run
, which handles signals and some asyncio event loop stuff automatically, overwriting what I was trying to do (which I think was being done wrong anyway). I have moved to usingclient.start
instead.