0

特定のタスクを実行するdjangoセロリビューがあり、タスクが正常に完了した後、それをデータベースに書き込みます。

私はこれをやっています:

result = file.delay(password, source12, destination)

と、

 if result.successful() is True:
      #writes into database

しかし、タスクの実行が終了した後、if条件にはなりません。試してみましresult.ready()たが、うまくいきませんでした。

編集:上記の行は同じビューにあります:

def sync(request):
    """Sync the files into the server with the progress bar"""
    choice = request.POST.getlist('choice_transfer')
    for i in choice:
        source12 = source + '/' + i 
        start_date1 = datetime.datetime.utcnow().replace(tzinfo=utc)
        start_date = start_date1.strftime("%B %d, %Y, %H:%M%p")

        basename = os.path.basename(source12) #Get file_name
        extension = basename.split('.')[1] #Get the file_extension
        fullname = os.path.join(destination, i) #Get the file_full_size to calculate size

        result = file.delay(password, source12, destination)

        if result.successful() is True:
             #Write into database

e:#データベースへの書き込み

4

1 に答える 1

1
  1. を呼び出すとfile.delay、セロリはタスクをキューに入れてバックグラウンドで実行します。

  2. すぐにチェックするresult.successful()と、タスクがまだ実行されていないため、falseになります。

タスクを連鎖させる必要がある場合(次々に実行する)、Celeryのワークフローソリューション(この場合は連鎖)を使用します。

def do_this(password, source12, destination):
    chain = file.s(password, source12, destination) | save_to_database.s()
    chain()


@celery.task()
def file(password, source12, destination):
    foo = password
    return foo


@celery.task()
def save_to_database(foo):
    Foo.objects.create(result=foo)
于 2013-02-25T08:55:46.130 に答える