<small id='5SEHH'></small><noframes id='5SEHH'>

    <bdo id='5SEHH'></bdo><ul id='5SEHH'></ul>
<legend id='5SEHH'><style id='5SEHH'><dir id='5SEHH'><q id='5SEHH'></q></dir></style></legend>
  • <tfoot id='5SEHH'></tfoot>
    1. <i id='5SEHH'><tr id='5SEHH'><dt id='5SEHH'><q id='5SEHH'><span id='5SEHH'><b id='5SEHH'><form id='5SEHH'><ins id='5SEHH'></ins><ul id='5SEHH'></ul><sub id='5SEHH'></sub></form><legend id='5SEHH'></legend><bdo id='5SEHH'><pre id='5SEHH'><center id='5SEHH'></center></pre></bdo></b><th id='5SEHH'></th></span></q></dt></tr></i><div id='5SEHH'><tfoot id='5SEHH'></tfoot><dl id='5SEHH'><fieldset id='5SEHH'></fieldset></dl></div>
      1. 如何在没有线程或任务队列的情况下在 Flask 中运行后台作业

        时间:2023-09-29

                <bdo id='pIjB1'></bdo><ul id='pIjB1'></ul>

                  <small id='pIjB1'></small><noframes id='pIjB1'>

                • <legend id='pIjB1'><style id='pIjB1'><dir id='pIjB1'><q id='pIjB1'></q></dir></style></legend>
                  <i id='pIjB1'><tr id='pIjB1'><dt id='pIjB1'><q id='pIjB1'><span id='pIjB1'><b id='pIjB1'><form id='pIjB1'><ins id='pIjB1'></ins><ul id='pIjB1'></ul><sub id='pIjB1'></sub></form><legend id='pIjB1'></legend><bdo id='pIjB1'><pre id='pIjB1'><center id='pIjB1'></center></pre></bdo></b><th id='pIjB1'></th></span></q></dt></tr></i><div id='pIjB1'><tfoot id='pIjB1'></tfoot><dl id='pIjB1'><fieldset id='pIjB1'></fieldset></dl></div>
                    <tbody id='pIjB1'></tbody>
                  <tfoot id='pIjB1'></tfoot>
                  本文介绍了如何在没有线程或任务队列的情况下在 Flask 中运行后台作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 Flask-restplus 构建 REST API.我的一个端点获取从客户端上传的文件并运行一些分析.该作业最多使用 30 秒.我不希望这项工作阻止主进程.因此端点将立即返回 200 或 201 的响应,作业仍然可以运行.结果将保存到数据库中,稍后检索.

                  I am building REST API with Flask-restplus. One of my endpoints takes a file uploaded from client and run some analysis. The job uses up to 30 seconds. I don't want the job to block the main process. So the endpoint will return a response with 200 or 201 right away, the job can still be running. Results will be saved to database which will be retrieved later.

                  对于长期运行的工作,我似乎有两种选择.

                  It seems I have two options for long-running jobs.

                  1. 线程
                  2. 任务队列

                  线程相对简单.但问题是,Flask 应用程序的线程数是有限制的.在独立的 Python 应用程序中,我可以为线程使用队列.但这是 REST api,每个请求调用都是独立的.我不知道是否有办法为此维护一个全局队列.所以如果请求超过线程限制,它将无法接受更多的请求.

                  Threading is relatively simpler. But problem is, there is a limit of thread numbers for Flask app. In a standalone Python app, I could use a queue for the threads. But this is REST api, each request call is independent. I don't know if there is a way to maintain a global queue for that. So if the requests exceed the thread limit, it won't be able to take more requests.

                  带有 Celery 和 Redis 的任务队列可能是更好的选择.但这只是概念验证,时间线有点紧.使用 Flask 设置 Celery、Redis 并不容易,我在 Windows 的开发机器上遇到了很多麻烦.它将部署在有点复杂的 AWS 上.

                  Task-queue with Celery and Redis is probably better option. But this is just a proof of concept thing, and time line is kind of tight. Setting up Celery, Redis with Flask is not easy, I am having lots of trouble on my dev machine which is a Windows. It will be deployed on AWS which is kind of complex.

                  我想知道这种情况是否有第三种选择?

                  I wonder if there is a third option for this case?

                  推荐答案

                  我强烈推荐使用 Celery 正如你在帖子中已经提到的那样.它正是为这个用例而构建的.他们的文档内容丰富,网上不乏可以帮助您快速启动和运行的示例.

                  I would HIGHLY recommend using Celery as you have already mentioned in your post. It is built exactly for this use case. Their docs are really informative and there are no shortage of examples online that can get you up and running quickly.

                  另外,我想说 THIS 将是您开始的绝佳第一个资源与.

                  Additionally, I would say THIS would be an excellent first resource for you to start with.

                  这篇关于如何在没有线程或任务队列的情况下在 Flask 中运行后台作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:蟒蛇-&gt;多处理模块 下一篇:在 python 中使用队列

                  相关文章

                  <i id='Ah14g'><tr id='Ah14g'><dt id='Ah14g'><q id='Ah14g'><span id='Ah14g'><b id='Ah14g'><form id='Ah14g'><ins id='Ah14g'></ins><ul id='Ah14g'></ul><sub id='Ah14g'></sub></form><legend id='Ah14g'></legend><bdo id='Ah14g'><pre id='Ah14g'><center id='Ah14g'></center></pre></bdo></b><th id='Ah14g'></th></span></q></dt></tr></i><div id='Ah14g'><tfoot id='Ah14g'></tfoot><dl id='Ah14g'><fieldset id='Ah14g'></fieldset></dl></div>

                    <bdo id='Ah14g'></bdo><ul id='Ah14g'></ul>
                • <tfoot id='Ah14g'></tfoot>

                  1. <legend id='Ah14g'><style id='Ah14g'><dir id='Ah14g'><q id='Ah14g'></q></dir></style></legend>

                    <small id='Ah14g'></small><noframes id='Ah14g'>