shayypy / guilded.py

Asynchronous Guilded API wrapper for Python
https://guildedpy.rtfd.io
Other
133 stars 25 forks source link

Embeed the bot inside a fastapi server #47

Closed NargiT closed 1 year ago

NargiT commented 1 year ago

Describe the bug Start my fastapi server using uvicorn, send a "ping" in chat, bot answer with "pong", stop uvicorn server. This is the stacktrace I get.

Ready
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [37840]
Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "C:\Users\xxx\IdeaProjects\onr-bot-guilded\.tox\py310\lib\site-packages\guilded\gateway.py", line 1022, in run
    f = asyncio.run_coroutine_threadsafe(coro, loop=self.ws.loop)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\asyncio\tasks.py", line 885, in run_coroutine_threadsafe
    loop.call_soon_threadsafe(callback)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 795, in call_soon_threadsafe
    self._check_closed()
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
python-BaseException
Exception in thread Thread-5:
Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "C:\Users\xxx\IdeaProjects\onr-bot-guilded\.tox\py310\lib\site-packages\guilded\gateway.py", line 1022, in run
    f = asyncio.run_coroutine_threadsafe(coro, loop=self.ws.loop)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\asyncio\tasks.py", line 885, in run_coroutine_threadsafe
    loop.call_soon_threadsafe(callback)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 795, in call_soon_threadsafe
    self._check_closed()
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
sys:1: RuntimeWarning: coroutine 'GuildedWebSocket.ping' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Process finished with exit code 0

To Reproduce

import asyncio
import guilded
from fastapi import FastAPI

app = FastAPI()
client = guilded.Client()

@app.on_event("startup")
async def startup_event():
    logging.info('Starting server')
    asyncio.create_task(client.start(TOKEN))

@client.event
async def on_ready():
    print('Ready')

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content == 'ping':
        await message.channel.send('pong!')

def main():
    import uvicorn
    uvicorn.run(app="app", host="0.0.0.0", port=8080, access_log=False, workers=1)

if __name__ == '__main__':
    main()

Expected behavior I don't expect any stack trace in logs

Actual behavior Stacktrace telling me RuntimeWarning: coroutine 'GuildedWebSocket.ping' was never awaited

Screenshots If applicable, please attach screenshots to help explain/demonstrate the problem.

Environment

shayypy commented 1 year ago

This is not really supported usage. I'm sure you could do this, but look into IPC instead.

NargiT commented 1 year ago

@shayypy I never had this issue with discord.py or pycord, this is why I open this issue.