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 多处理模块


基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01