Django:在后台线程中启动一个进程? [英] Django: start a process in a background thread?

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

问题描述

我正在尝试解决如何在Django的后台线程中运行一个进程。我是Django和线程的新手,所以如果我使用的术语错了,请与我保持联系。

I'm trying to work out how to run a process in a background thread in Django. I'm new to both Django and threads, so please bear with me if I'm using the terminology wrong.

这是我的代码。基本上,只要成功功能被触发,我希望 start_processing 开始。然而, start_processing 是一种可以轻松占用几分钟或失败的功能(它取决于我无法控制的外部服务),我不想用户必须等待它在渲染视图之前成功完成。 (成功)不依赖于 start_processing 的结果;我是唯一需要担心的人,如果失败。)

Here's the code I have. Basically I'd like start_processing to begin as soon as the success function is triggered. However start_processing is the kind of function that could easily take a few minutes or fail (it's dependent on an external service over which I have no control), and I don't want the user to have to wait for it to complete successfully before the view is rendered. ('Success' as far as they are concerned isn't dependent on the result of start_processing; I'm the only person who needs to worry if it fails.)

def success(request, filepath):
    start_processing(filepath)
    return render_to_response('success.html', context_instance = RequestContext(request))

从Google Googling中,大多数人建议背景线程不在Django中使用,而cron作业更适合。但是,一旦用户进入成功函数,我就会喜欢 start_processing 开始,而不是等到cron作业运行。有没有办法这样做?

From the Googling I've done, most people suggest that background threads aren't used in Django, and instead a cron job is more suitable. But I would quite like start_processing to begin as soon as the user gets to the success function, rather than waiting until the cron job runs. Is there a way to do this?

推荐答案

我不知道你需要一个线程。听起来你只想产生一个过程,所以看看子进程模块。

I'm not sure you need a thread for that. It sounds like you just want to spawn off a process, so look into the subprocess module.

这篇关于Django:在后台线程中启动一个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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