RuntimeError: There is no current event loop in thread in flask?

Why I am getting this error When I run my code?

Actually I want to run autoreminder.py when I start my flask app and autoreminder.py should be callable in background after every 10 second

autoreminder.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]
    getloop.run_until_complete(asyncio.gather(*coroutines))
    getloop.close()

app.py

from flask import Flask, request,render_template
from apscheduler.schedulers.background import BackgroundScheduler
from autoreminder import main

app = Flask(__name__)


scheduler = BackgroundScheduler()
job = scheduler.add_job(main, 'interval', seconds=10)
scheduler.start()


# Starting app
if __name__ == "__main__":
    app.run(threaded=True)

Error

RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'.



Read more here: https://stackoverflow.com/questions/65840058/runtimeerror-there-is-no-current-event-loop-in-thread-in-flask

Content Attribution

This content was originally published by Bhavesh Mevada at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: