Skip to content

Commit

Permalink
Only use app.before_serving for Quart apps
Browse files Browse the repository at this point in the history
While this lib doesn't officially support async with Flask, it is possible, and this is a step towards allowing it
  • Loading branch information
breqdev committed Aug 15, 2021
1 parent 3b2af70 commit 625ee52
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions flask_discord_interactions/discord.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import time
import inspect
import uuid
import atexit
import asyncio

import requests

Expand Down Expand Up @@ -523,13 +525,23 @@ async def interactions():

# Set up the aiohttp ClientSession

@app.before_serving
async def create_session():
app.discord_client_session = aiohttp.ClientSession(
headers=self.auth_headers(app),
raise_for_status=True
)

@app.after_serving
async def close_session():
await app.discord_client_session.close()

if hasattr(app, "before_serving"):
# Quart apps
app.before_serving(create_session)
app.after_serving(close_session)
else:
# Flask apps
app.before_first_request(create_session)
atexit.register(
lambda: asyncio.get_event_loop().run_until_complete(
close_session())
)

0 comments on commit 625ee52

Please sign in to comment.