【问题标题】:How server checks if the request from client is still active to the flask api?服务器如何检查来自客户端的请求对烧瓶 api 是否仍然有效?
【发布时间】:2019-07-02 13:16:22
【问题描述】:

我有一个烧瓶 api,它执行一些功能过程,并且需要几分钟才能返回。有时请求会被客户端中止。在这些情况下,我想取消处理。我怎样才能在烧瓶中做到这一点?

我找到了一个临时解决方案。

@app.route('/stream')
    def stream():
       def gen():
           try:
               i = 0
               while True:
                   data = 'this is line {}'.format(i)
                   print(data)
                   yield data + '<br>'
                   i += 1
                   time.sleep(1)
           except GeneratorExit:
               print('closed')

return Response(stream_with_context(gen()))

现在,当我们通过 sublime 请求时,这适用于浏览器,一段时间后我们关闭 sublime,flask api 无法识别客户端终止

【问题讨论】:

    标签: python flask sublimetext3 mod-wsgi wsgi


    【解决方案1】:

    使用 celery 包,并将该功能作为任务运行。然后,您可以使用任何其他途径来启动 revoke() 函数,该函数会停止 celery 任务,例如。 your_worker.revoke(task_id, terminate=True)。

    【讨论】:

    • 我们可以在 celery 任务中使用多处理函数吗?
    • Celery 是关于多处理的。您可以为每个工作人员添加多个任务,然后一个一个地启动/停止它们,或者一次全部启动/停止。
    猜你喜欢
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多