为什么烧瓶应用程序需要这么长时间才能运行?

时间:2020-05-12 08:47:58

标签: python flask topic-modeling python-newspaper

这是我的主页,需要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)

1 个答案:

答案 0 :(得分: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应用程序投入生产。 Flask官方文档中的一些指南:Deploy to ProductionGunicorn with NGINX是一个很好的选择。