RuntimeError: There is no current event loop in thread 'Thread-1' in flask

拥有回忆 提交于 2021-02-05 09:46:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!