I created a flask framework for debugging the API, and when calling a function with an asynchronous function an error occurs.
RuntimeError: There is no current event loop in thread 'Thread-2 (process_request_thread)'.
I tried to simplify the same problem, and found that calling an asynchronous function with flask always throws an error, while running the API function directly does not.
Here is my code.
app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def call_func():
print("I will call function package.normal_func")
import package
package.normal_func()
call_func()
app.run(debug=True)
package / __init__.py
import asyncio
async def async_func():
print("Async function there")
def normal_func():
print("I am normal function")
print("I will call async function")
asyncio.get_event_loop().run_until_complete(async_func())
I have simplified the problem, and I expect to be provided with a simple and straightforward description of the problem and a workable code. I’ve asked ChatGPT and its answer is not clear.