【问题标题】:How to update table in database while file is being transferred using django celery?使用 django celery 传输文件时如何更新数据库中的表?
【发布时间】:2013-02-04 11:59:21
【问题描述】:

我在 Django 中有这样的任务:

from celery import task
import subprocess, celery

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

这会使用rsync 将文件从一台服务器传输到另一台服务器。 以下是我的看法:

def sync(request):
    """Sync the files into the server with the progress bar"""
    choices = request.POST.getlist('choice') 
    for i in choices:
        new_source = source +"/"+ i 
        start_date1 = datetime.datetime.utcnow().replace(tzinfo=utc)
        source12 = new_source.replace(' ', '') #Remove whitespaces
        result = file.delay(password, source12, destination)
        result.get()
        a = result.ready()
        start_date = start_date1.strftime("%B %d, %Y, %H:%M%p")

        extension = os.path.splitext(i)[1][1:] #Get the file_extension
        fullname = os.path.join(destination, i) #Get the file_full_size to calculate size
        st = int(os.path.getsize(fullname))
        f_size = size(st, system=alternative)

我想更新我想要更新的数据库中的表并将其显示给用户。传输文件时应更新表。如何使用django-celery 做到这一点?

【问题讨论】:

    标签: django django-celery


    【解决方案1】:

    对于 Django,Celery 并没有什么特别之处。您可以像往常一样更新数据库。您可能需要考虑的唯一一件事就是交易。

    为了确定我会推荐使用手动提交或自动提交来更新数据库。尽管我建议使用 redis/memcached 而不是数据库来进行此类状态更新。它们更适合此目的。

    【讨论】:

      猜你喜欢
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 2016-12-11
      • 2021-08-20
      • 2021-11-11
      相关资源
      最近更新 更多