问题
I want to run a asynchronous event in background with flask and for that i worte a code below But When I run app.py main function is successfully run for the first time by the schedule but in second time it throwing an error.
Please give me a solution
main.py
import asyncio
import aiohttp
from database import DBHelper
MESSAGE_TO_SEND = 'Please Complete Your Task'
ACCESS_TOKEN = Access_token
db = DBHelper()
async def taskReminder(memberID):
data = {
"recipient": { "id": memberID },
"message": { "text": MESSAGE_TO_SEND }
}
async with aiohttp.ClientSession() as session:
async with session.post("https://graph.facebook.com/v8.0/me/messages?access_token="+ ACCESS_TOKEN, json=data) as response:
return await response.text()
def main():
loop = asyncio.get_event_loop()
Members = db.loadMembersNotCompletedTask()
coroutines = [taskReminder(''.join(member)) for member in Members]
loop.run_until_complete(asyncio.gather(*coroutines))
app.py
from flask import Flask, request,render_template
import schedule
from threading import Thread
from autoreminder import main
app = Flask(__name__)
def run_schedule():
while 1:
schedule.run_pending()
time.sleep(1)
# Starting app
if __name__ == "__main__":
schedule.every(10).seconds.do(main)
t = Thread(target=run_schedule)
t.start()
app.run(threaded=True)
来源:https://stackoverflow.com/questions/65824529/runtimeerror-there-is-no-current-event-loop-in-thread-thread-1-in-flask