我可以在Django中运行后台进程而无需启动并行进程吗? [英] Can I run a background process in Django without starting a parallel process?

查看:58
本文介绍了我可以在Django中运行后台进程而无需启动并行进程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常幼稚的问题,但是我觉得我对django和python中的异步/后台任务不了解一些基本知识.

This is a very naive question, but I feel I don't understand something fundamental about asynchronous/background tasks in django and python.

我尝试复制django-background-tasks( https://github.com/collinmutembei/django-background-tasks-example ),以使django比实际运行晚60秒执行后台任务.但是我想对于其他任何后台任务管理器(例如Celery或Huey)也一样.

I try to replicate a simple example provided by django-background-tasks (https://github.com/collinmutembei/django-background-tasks-example) in order to make django perform a background task 60 seconds later than it was actually run. But I guess the same is valid for any other background task manager such as Celery or Huey

这个例子非常简单-用户访问网址后,就会执行一个简单的打印消息功能,而不会阻塞60秒后的django主进程.

The example is pretty simple - as soon as the user accesses the url, a simple function that prints a message is executed without blocking the main django process, 60 sec later

  from background_task import background
  from logging import getLogger

  logger = getLogger(__name__)

  @background(schedule=60)
  def demo_task(message):
      logger.debug('demo_task. message={0}'.format(message))

问题是我真的不了解基本知识.除非我启动一个单独的(或分离的)进程 python manage.py process_tasks ,否则它不会运行.我应该总是这样做以使后台任务正常工作,还是有一种无需启动并行进程就可以做到这一点的方法?

The problem is that I really don't understand the basics. It doesn't run unless I start a separate (or detached) process python manage.py process_tasks. Should I always do it to make the background task work, or there is a way to do it without starting a parallel process?

如果我应该开始并行处理,可以在Django代码内部执行.像这样:

If I should start a parallel process, can I do it from inside of django code. Something like:

    import subprocess

    process = subprocess.Popen(['python', 'manage.py','process_tasks'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

推荐答案

运行一个单独的进程以在后台运行任务不是必需的,但有益且有用.

It is not necessary but good and helpful to run a separate process to run tasks in the background.

运行服务器时,将创建一个进程-运行 ps aux |grep runserver -负责处理Web请求.当您说要在后台运行某些任务时,它暗含着您要一个单独的进程来执行那些任务.这是诸如celery之类的异步任务工具出现的地方.

When you run a server, a process is created - run ps aux | grep runserver - which is responsible for serving web requests. When you say that you want to run certain tasks in the background, it implicitly means that you want a separate process to execute those tasks. This is where asynchronous task tools like celery come in.

您也可以按照以下说明自己生成单独的进程:

You can also spawn a separate process yourself - as you said - by doing:

import subprocess

process = subprocess.Popen(['python', 'manage.py','process_tasks'], stdout=subprocess.PIPE, stderr=subprocess.PIPE

如果您只有一个或两个要并行运行的小任务,则此方法也完全可以.但是,当您有成千上万的复杂任务在后台运行时,您将需要对其进行适当的管理.另外,如果出现问题,您还需要能够调试这些任务.稍后,您将需要更多了解所有后台任务中发生的情况,它们的状态等.这是芹菜可以为您提供帮助的地方.它将为您提供经过修饰的方法,这些方法将为您处理所有这些事情.然后您只需要担心您的业务逻辑

This method is also completely fine if you have just one or two small tasks that you want to run in parallel. However, when you have tons of complicated tasks that you are running in the background, you would want to manage them properly. Also, you need to be able to debug those tasks, if something goes wrong. Later, you will need more visibility into what is happening in all the background tasks, their status, etc. This is where celery will help you. It will give you decorated methods which will handle all those things for you. You just have to worry about your business logic then

这篇关于我可以在Django中运行后台进程而无需启动并行进程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆