How can I concurrently run a Flask-based webhook server and a Discord bot in one Python script?
from flask import Flask; import discord
app = Flask(__name__); client = discord.Client()
app.route('/')(lambda: 'ok'); client.run('YOUR_TOKEN')
How can I concurrently run a Flask-based webhook server and a Discord bot in one Python script?
from flask import Flask; import discord
app = Flask(__name__); client = discord.Client()
app.route('/')(lambda: 'ok'); client.run('YOUR_TOKEN')
In my experience, running Flask and a Discord bot concurrently in a single script can be effectively managed by using a background thread for the Flask application. I implemented this by starting Flask in a separate thread, which then allowed the Discord client to run in the main thread. This approach helped prevent the blocking of one service by the other. I found that although it may seem simple, handling both services within the same script does require careful attention to shared resources and error handling in case one of the event loops encounters issues. It worked reliably for me in production.
hey alice45, i’ve tinkered with combining both via an asyncio loop, running webhooks and the bot concurrently. its a bit messy without thread but it works ok. hope this helpz!