4

我正在制作一个简单的烧瓶应用程序,它从外部网站返回结果。用户将数据输入我的网站。此数据用于加载另一个站点。数据被提取并以列表的形式返回。该程序独立工作,但不能作为烧瓶应用程序的一部分工作。我已经尝试使用 requests_HTML 库使用它的异步工具,并且我尝试使用 Pyro4 将请求发送到外部进程。但我总是想出这个错误的一些版本:

RunTimeError:线程中没有当前事件循环......

在我看来,当我的一个导入模块运行一个线程时,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 文件中尝试了这段代码,它工作正常。(即在烧瓶应用程序之外,只需将结果打印到命令行)

这是来自烧瓶调试器的回溯:

'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()
4

0 回答 0