I am making a simple flask app that returns results from an external website. The user enters data into my site. This data is used to load another site. Data is extracted and returned in a list. This program works independently, but will not work as part of the flask app. I have tried using using requests_HTML library using it's async tools, and i have tried using Pyro4, to send the request to an external process. But i always come up with some version of this error:
RunTimeError: There is no current event loop in thread ........
It seems to me that when one of my imported modules runs a thread Flask doesn't like it. I would love to know why does this happen, is their something in flasks inner workings that means that it won't work well with threads or async or something? Any direction to some extra resources or info would be very appreciated
This is my Flask App:
from flask import Flask, render_template
from requests_html import AsyncHTMLSession
app = Flask(__name__)
asession = AsyncHTMLSession()
URLS=["https://stackoverflow.com/questions/",
"https://stackoverflow.com/questions/",
"https://stackoverflow.com/questions/"]
@app.route("/", methods=('GET'))
def index():
results = asession.run(run_requests)
return render_template('index_page.html', results_list=results)
async def run_requests():
results_list=[]
for url in URLS:
results_list.append(await get_and_render(url))
return results_list
async def get_and_render(url):
r = await asession.get(url)
await r.html.arender(sleep=0.75)
summary = r.html.find(".question-hyperlink", first=True)
return summary.text
This will get the stackoverflow questions page and grab the summary of the last posted question. I have tried this code in a standalone python file and it works fine. (ie outside of a flask app, just printing the results to the command line)
This is by traceback from the flask debugger:
'Traceback (most recent call last):
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\......\StackOverflowExample.py", line 11, in index
results = asession.run(run_requests)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\requests_html.py", line 771, in run
tasks = [
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\requests_html.py", line 772, in <listcomp>
asyncio.ensure_future(coro()) for coro in coros
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\asyncio\tasks.py", line 660, in ensure_future
loop = events.get_event_loop()
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\asyncio\events.py", line 639, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-4'.
Can anyone explain why flask has trouble completing these tasks?
cheers
PS here is my simple HTML (although app has not been able to render this yet.):
<ul>
{% for result in results_list %}
<li>{{result}}</li>
{% endfor %}
</ul>
Here is my Script outside the flask app(it works):
from requests_html import AsyncHTMLSession
asession = AsyncHTMLSession()
URLS=["https://stackoverflow.com/questions/",
"https://stackoverflow.com/questions/",
"https://stackoverflow.com/questions/"]
def index():
results = asession.run(run_requests)
for result in results:
print(result)
async def run_requests():
results_list=[]
for url in URLS:
results_list.append(await get_and_render(url))
return results_list
async def get_and_render(url):
r = await asession.get(url)
await r.html.arender(sleep=0.75)
summary = r.html.find(".question-hyperlink", first=True)
print(summary.text)
if __name__ == "__main__":
index()