我正在制作一个简单的flask应用程序,该应用程序从外部网站返回结果。用户将数据输入我的站点。此数据用于加载另一个站点。提取数据并以列表形式返回。该程序可独立运行,但不能作为flask应用程序的一部分运行。我尝试使用它的异步工具来使用requests_HTML库,并且尝试使用Pyro4将请求发送到外部进程。但是我总是想出这个错误的一些版本:
RunTimeError:线程........
中没有当前事件循环在我看来,当我导入的模块之一运行线程时,Flask不喜欢它。 我想知道为什么会发生这种情况,它们是否在烧瓶内部起作用,这意味着它不能与线程或异步或某些东西一起很好地工作? 指向一些额外资源或信息的任何方向将非常感谢
这是我的Flask应用程序:
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
这将显示stackoverflow问题页面,并获取最后发布的问题的摘要。我已经在独立的python文件中尝试过此代码,并且可以正常工作。 (即在flask应用程序外部,只需将结果打印到命令行即可)
这是通过烧瓶调试器进行的追溯:
'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'.
谁能解释为什么烧瓶在完成这些任务时遇到麻烦?
欢呼
PS 这是我的简单HTML(尽管应用程序尚无法呈现它。):
<ul>
{% for result in results_list %}
<li>{{result}}</li>
{% endfor %}
</ul>
这是我的烧瓶应用程序外部的脚本(有效):
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()