Flask-异步渲染模板 [英] Flask - render template asynchronously

查看:138
本文介绍了Flask-异步渲染模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Flask应用程序,我想知道是否可以为路由渲染一个模板,但是在功能完成后重定向用户.当前使用的是Python 2.7,这是我的示例

I'm making a Flask app and I was wondering if I could render a template for a route, but redirect the user after a function is complete. Currently using Python 2.7 Here is my example

@app.route('/loading/matched')
def match():
    time_match()
    return render_template('matched.html')

def time_match():
    # match two users based on time
    sleep(3) # pretend to be doing
    return redirect('/loading/generation')

我不知道从哪里开始.我应该使用图书馆吗?

I don't know where to begin. Is there a library I should use?

推荐答案

在我看来,这听起来像是客户端的事情?您是否想要类似加载栏的东西?

This sounds more like a client sided thing to me? Do you want something like a loading bar?

您可以提供一个ajax路由,该路由会在服务器端启动繁重的工作量-而客户端确实显示出一定的进度.工作负载完成后,您将呈现一个模板,然后再通过ajax加载该模板.

You could provide an ajax route which initiates heavy workload on the server side - while the client does show some progress. Once the workload finished you render a template which than gets loaded via ajax.

对于asycn工作负载,您可以查看 Celery ,这是一个很棒的库.它甚至可以在单独的服务器上工作...

For asycn workload you could look into Celery, which is a great library for that. It even can do work on a seperate server...

更多资源与Flask集成

这篇关于Flask-异步渲染模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆