Flask:当用户在执行较长的过程时关闭浏览器时会发生什么? [英] Flask: What happens when a user closes the browser while a long process is being executed?

查看:42
本文介绍了Flask:当用户在执行较长的过程时关闭浏览器时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况如下:

我有一个应用程序,其前端使用Angularjs,后端使用Flask.

I have an app which uses Angularjs for the front-end and Flask for the back-end.

我的路线看起来像:

@app.route('/api/route1', methods=['POST'])
def route1():
    result = some_package.long_task()
    result2 = some_package.short_task(result)
    return jsonify(result)

long_task 函数正在使用 check_output 执行一些bash命令,使用数据库,读写文件等等.可能需要几个小时才能完成.

The long_task function is executing some bash commands using check_output, using the database, reading and writing files and so on. It can take a couple of hours to get finished.

假设用户厌倦了等待,并在该过程开始10分钟后关闭了浏览器窗口.

Let's say that the user gets tired of waiting and closes the browser window 10 minutes after the process started.

我的问题是:

  • long_task short_task 是否都将被执行?
  • 这种情况是否可能造成任何内存泄漏?
  • 是否有可能知道用户在某个时候关闭了浏览器?(包括当我尝试返回响应时:是否有可能知道响应未传递?)
  • 这个问题的答案是否取决于我使用的服务器?(龙卷风,uWSGI ...)

非常感谢您的回答.

推荐答案

  1. long_task和short_task都将被执行吗?

是的,他们会的.Flask不知道客户端是否关闭了连接.

Yes, they will. Flask doesn't know if connection closed by the client.

  1. 这种情况是否可能造成任何内存泄漏?

是的,但这仅取决于long_task和short_task中的代码.即使没有连接抛出,也会发生内存泄漏.您可以写入记录请求前后分配的内存之间的差异.

Yes, but it depends only on your code in long_task and short_task. Memory leaks can occur even if there is no connection throw. You can write to log difference between allocated memory before and after request.

  1. 是否有可能知道用户在某个时候关闭了浏览器?(包括当我尝试返回响应时:可能知道未收到答复?)答案是否正确?这个问题取决于我使用的是哪个服务器?(龙卷风,uWSGI ...)

简单答案-不.但这可以通过执行long_task时通过流式传输空响应进行某种黑客攻击并捕获异常(如果客户端关闭连接将断言)来实现.

Simple answer - no. But it can be done with some sort of hacking with streaming empty response while long_task is executed and catching exception which will be asserted if client close connection.

您可以在此处阅读以下内容:如果请求中止,请停止处理Flask路由

You can read about it here: Stop processing Flask route if request aborted

这篇关于Flask:当用户在执行较长的过程时关闭浏览器时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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