0

这是我的主页,运行需要 30 秒。有很多图,基于数据集的wordcloud有大约1000篇文章和一些关于sqlalchemy的基本操作。但它仍然不应该花费那么多时间。我怎样才能减少时间?

@app.route('/home',methods=["get","post"])
def showjson():
    folder = 'C:/Users/Mansi Dhingra/Desktop/Projects/api/news/static/images'
    for filename in os.listdir(folder):
        file_path = os.path.join(folder, filename)
        os.remove(file_path)

    news_df = pd.read_csv('news_information1.csv')
    news_df.to_sql('users', con=engine)
    topic_l = engine.execute('''Select distinct Topic from users''').fetchall()
    topic_list=[]
    for tr in topic_l:
        topic_list.append(tr[0])
    search = request.form.get("search")
    source_l=engine.execute('''Select distinct source from users''').fetchall()
    source_list = []
    for tr in source_l:
        source_list.append(tr[0])
    bank_l = engine.execute('''Select distinct bank from users''').fetchall()
    bank_list = []
    for tr in bank_l:
        bank_list.append(tr[0])
    end_date = engine.execute('''Select max(date) from users''').fetchall()

    max_date=end_date[0][0]
    sent_count = engine.execute('''Select Sentiment,Count(*) from users group by Sentiment''').fetchall()
    sent_topic = []
    sent_count1 = []
    for tx in sent_count:
        sent_topic.append(tx[0])
        sent_count1.append(tx[1])
    fig_sent=create_graphs(sent_topic,sent_count1,"sentiment")
    list_words = fetch_sentiment_using_vader(news_df['clean_text'])
    stopwords = stopwords_for_wordcount(news_df['clean_text'])
    count_vectorizer = CountVectorizer(stop_words=stopwords[0])
    fig_pos=plot_words(list_words[0], list_words[2], "positive")
    fig_neg=plot_words(list_words[1], list_words[2], "negative")
    fig_cat=count_category(news_df)
    fig_pub=count_pub(news_df)
    create_wordcloud( stopwords)
    fig_tri=bigram_or_trigram(news_df['clean_text'], stopwords,"bigram")
    images_list = os.listdir(os.path.join(app.static_folder, "images"))
    return render_template('news_home.html',fig_pub=fig_pub,topic_list=topic_list,img=images_list,plt_pos=fig_pos,plt_tri=fig_tri,plt_neg=fig_neg,
                           bank_list=bank_list,source_list=source_list,max_date=max_date,fig_cat=fig_cat,fig_sent=fig_sent,search=search)

4

1 回答 1

0

如果您使用的是 Flask 1.0 之前的版本,请启用多线程。例如app.run(threaded=True)。否则,如果您的版本高于 1.0,那么您可以使用 Flask 的默认 WSGI 工具包 Werkzeug 分析器来为每个请求查找昂贵的函数。

from werkzeug.middleware.profiler import ProfilerMiddleware
app = ProfilerMiddleware(app, restrictions=[20])

执行上述操作将显示每个请求的 20 个最昂贵的函数。检查它们并自己修改/重构。

如果您正在考虑将其投入生产,这只是一个提示,默认 Flask 尚未准备好生产。将烧瓶应用程序投入生产还有一些额外的步骤。官方 Flask 文档中的一些指导:部署到生产带有 NGINX 的 Gunicorn是一个非常好的选择。

于 2020-07-03T06:35:21.877 回答