【问题标题】:Show file information that is transferring using Django celery?显示使用 Django celery 传输的文件信息?
【发布时间】:2013-02-05 05:46:45
【问题描述】:

我在 Django celery 中有这样的任务:

@task
def file(password, source12, destination):
    subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', source12, destination], 
                                    stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]

我有一个执行上述任务的函数:

@celery.task
@login_required(login_url='/login_backend/') 
def sync(request):
    #user_id = request.session['user_id']
    """Sync the files into the server with the progress bar"""
    if request.method == 'POST':
        choices = request.POST.getlist('choice') 
        for i in choices:
            new_source = source +"/"+ i 
            #b = result.successful()
            #result.get() #Poll the database to get the progress
            start_date1 = datetime.datetime.utcnow().replace(tzinfo=utc)
            source12 = new_source.replace(' ', '') #Remove whitespaces
            file.delay(password, source12, destination)
        return HttpResponseRedirect('/uploaded_files/')
    else:
        return HttpResponseRedirect('/uploaded_files/')

我想向用户显示文件传输的进度信息。我想显示正在传输的file name, remaining time and size of the file。我该怎么做?

【问题讨论】:

    标签: django django-celery


    【解决方案1】:

    这里有example 了解如何通过 celery 本身传递信息。只需以 AJAX 调用的形式启动上传过程,返回任务的 ID,然后通过 AJAX 询问任务的状态。

    要真正了解您的工作情况,您需要了解file 任务本身的进度。我怀疑您是否可以通过以其他方式调用子进程sshpass 然后解析PIPE 输出来实现这一点。

    当你.delay 一个任务时,Mind celery 已经为你生成了一个进程。

    尝试以块的形式读取目标,rsync 块,然后合并目标中的文件。这样你就知道你已经转移了多少块。

    【讨论】:

      猜你喜欢
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多