<legend id='VrAWn'><style id='VrAWn'><dir id='VrAWn'><q id='VrAWn'></q></dir></style></legend>

  • <small id='VrAWn'></small><noframes id='VrAWn'>

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

        <tfoot id='VrAWn'></tfoot>
      1. 如何在 django 视图中使用 python 多处理模块

        时间:2023-08-06
            <tbody id='aZZ1I'></tbody>
            <bdo id='aZZ1I'></bdo><ul id='aZZ1I'></ul>

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

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

                  <tfoot id='aZZ1I'></tfoot>

                1. 本文介绍了如何在 django 视图中使用 python 多处理模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个简单的函数来遍历 URL 列表,使用 GET 来检索一些信息并相应地更新 DB (PostgresSQL).该功能完美运行.但是,一次一个地浏览每个 URL 会占用太多时间.

                  I have a simple function that go over a list of URLs, using GET to retrieve some information and update the DB (PostgresSQL) accordingly. The function works perfect. However, going over each URL one at a time talking too much time.

                  使用 python,我可以执行以下操作来并行执行这些任务:

                  Using python, I'm able to do to following to parallel these tasks:

                  from multiprocessing import Pool
                  
                  def updateDB(ip):
                       code goes here...
                  
                  if __name__ == '__main__':
                      pool = Pool(processes=4)              # process per core
                      pool.map(updateDB, ip)
                  

                  这工作得很好.但是,我试图找到如何在 django 项目上做同样的事情.目前我有一个函数(视图),可以遍历每个 URL 以获取信息并更新数据库.

                  This is working pretty well. However, I'm trying to find how do the same on django project. Currently I have a function (view) that go over each URL to get the information, and update the DB.

                  我唯一能找到的就是使用 Celery,但这对于我想要执行的简单任务来说似乎有点过于强大了.

                  The only thing I could find is using Celery, but this seems to be a bit overpower for the simple task I want to perform.

                  有什么简单的我可以做或者我必须使用 Celery 吗?

                  Is there anything simple that i can do or do I have to use Celery?

                  推荐答案

                  目前我有一个函数(视图)可以遍历每个 URL 以获取信息,并更新数据库.

                  Currently I have a function (view) that go over each URL to get the information, and update the DB.

                  这意味着响应时间对您来说并不重要,而不是在后台(异步)执行,如果您的响应时间减少 4(使用 4 个子进程/线程),您可以在前台执行.如果是这种情况,您可以简单地将示例代码放在您的视图中.喜欢

                  It means response time does not matter for you and instead of doing it in the background (asynchronously), you are OK with doing it in the foreground if your response time is cut by 4 (using 4 sub-processes/threads). If that is the case you can simply put your sample code in your view. Like

                  from multiprocessing import Pool
                  
                  def updateDB(ip):
                       code goes here...
                  
                  def my_view(request):
                      pool = Pool(processes=4)              # process per core
                      pool.map(updateDB, ip)
                      return HttpResponse("SUCCESS")
                  

                  但是,如果您想在后台异步执行此操作,那么您应该使用 Celery 或遵循@BasicWolf 的建议之一.

                  But, if you want to do it asynchronously in the background then you should use Celery or follow one of @BasicWolf's suggestions.

                  这篇关于如何在 django 视图中使用 python 多处理模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 Python 将巨大的嵌套循环划分为 8 个(或更多 下一篇:Python 中的 ProcessPoolExecutor 和 Lock

                  相关文章

                  最新文章

                  <tfoot id='ImFVC'></tfoot>

                    <legend id='ImFVC'><style id='ImFVC'><dir id='ImFVC'><q id='ImFVC'></q></dir></style></legend>

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

                    1. <small id='ImFVC'></small><noframes id='ImFVC'>