1

我正在尝试在 Django 中获取 start_date 和 end_date(处理完成后)。我有一个简单的表格。当用户单击时Submit,我想获取 start_date 并将该值插入数据库。处理完成后,我想获取 end_date 并将该值插入数据库。我已经在 Django 中设置了数据库。处理如下:

command = subprocess.Popen(['sshpass', '-p', password, 'rsync', '--recursive', source],
                           stdout=subprocess.PIPE)

我如何知道命令执行何时完成并记录 end_date?

4

2 回答 2

0

You are looking for Popen.poll(). It will return None while the Popen is still executing. By example:

import subprocess
from datetime import datetime

d1 = datetime.now()
c = subprocess.Popen (["find", "/home/whatever", "-name","eclipse.ini"])

while c.poll() is None:
    pass

deltatime = datetime.now() - d1
print deltatime.total_seconds()

Be careful with the while loop. Your program will be stuck in it until the Popen process ends. Maybe you want to launch a thread to avoid that.

于 2013-01-03T12:04:46.423 回答
0

如果您想以优雅的方式进行操作,您应该尝试使用异步任务系统(如celery),并在 César García 提供的任务中执行 bash 进程。

另一种选择是执行如下操作:

subprocess.Popen(['sshpass ... && curl -X GET IP:PORT/some/internal/view/%d/' % task_id], shell=True)

完成时间可以插入到 django 视图内的数据库中。

于 2013-01-03T13:04:56.540 回答