简体   繁体   English

在Django视图中使用subprocess.Popen()执行python脚本

[英]Executing a python script using subprocess.Popen() in a django view

I've looked around a bit but I can't seem to solve this problem I have. 我环顾了一下,但似乎无法解决这个问题。 I'd like to execute a python script within a view of my django app. 我想在我的django应用程序视图中执行python脚本。 I've placed the code I'd like to execute inside a django management command so it can be accessed via command line python manage.py command-name . 我将要执行的代码放在django管理命令中,以便可以通过命令行python manage.py command-name对其进行访问。 I then tried to run this command using subprocess.Popen("python manage.py command-name",shell=True) . 然后,我尝试使用subprocess.Popen("python manage.py command-name",shell=True)运行此命令。

However, this command could take some time to execute so I'd like the view to continue and allow the script to execute in the background. 但是,此命令可能需要一些时间才能执行,因此我希望视图继续并允许脚本在后台执行。 Using subprocess.Popen alone seems to cause the view to hang until the script has finished, so I tried using a thread (following another SA question): 仅使用subprocess.Popen似乎会导致视图挂起,直到脚本完成为止,因此我尝试使用线程(接着另一个 SA问题):

class SubprocessThread(threading.Thread):
def __init__(self, c):
    self.command = c
    self.stdout = None
    self.stderr = None
    threading.Thread.__init__(self)

def run(self):
    p = subprocess.Popen(self.command,
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    self.stdout, self.stderr = p.communicate()

and then executing it: 然后执行它:

t = SubprocessThread("python manage.py command-name")
t.setDaemon(True)
t.start()
t.join()

However, the view still hangs: the cursor has a busy symbol and the AJAX on the page does not load. 但是,视图仍然挂起:光标具有繁忙的符号,并且页面上的AJAX没有加载。 Otherwise the page's html seems to load fine and commands in the view after the thread call appear to finish normally (before the script finishes). 否则,页面的html似乎可以正常加载,并且线程调用后的视图中的命令似乎正常完成(在脚本完成之前)。 Can someone please help me? 有人可以帮帮我吗? I'd like the script to execute and do its own thing without holding up the view or AJAX calls on the page. 我希望脚本能够执行并做自己的事情,而无需在页面上按住视图或AJAX调用。

Maybe you should use celery 也许你应该用芹菜

Celery is a task queue/job queue based on distributed message passing. Celery是基于分布式消息传递的任务队列/作业队列。 It is focused on real-time operation 它专注于实时操作

I wasted a lot of time trying to implement something similar, but had the same problems as you. 我浪费了很多时间来尝试实现类似的东西,但是遇到了与您相同的问题。 Eventually, I gave up and implemented a beanstalk queue to handle the work. 最终,我放弃并实现了一个beantalk队列来处理工作。

http://kr.github.com/beanstalkd/ http://kr.github.com/beanstalkd/

I put an id on the queue in the Django view, and then have a management command to run the consumer (watched by supervisord). 我在Django视图中的队列上放置了一个id,然后有一个管理命令来运行使用者(由超级用户监视)。

Using a queue means you could expand to multiple consumers, and allows to better manage the load (pausing the consumer if necessary without losing the work required). 使用队列意味着您可以扩展到多个使用者,并可以更好地管理负载(必要时暂停使用者,而不会丢失所需的工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM