How to use python multiprocessing module in django view(如何在 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 多处理模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 django 视图中使用 python 多处理模块
基础教程推荐
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
