1
セロリを実行している2つのサーバーがあります。別のサーバーでセロリタスクのステータスを取得しています
1 - Webサーバー 2 - 労働者のサーバー
Webサーバは、労働者のサーバーにタスクを送信するための責任があります。作業者はこれらの作業を行います。
これらのタスクのステータスをWebサーバーで監視したいと考えています。何か案は?あなたはDjangoのデータベースにタスクの結果を保存するためにセロリを設定する必要が
views.py
@user_passes_test(lambda u: u.is_superuser)
def taskCompute(request, uuid):
"""
Now we have the uuid that identifies the image over we want to apply the process as an input.
To trigger this view, the url has an uuid, and we receive it to receive the image we are working with
"""
doc = AnnotationDocument.objects.get(uuid=uuid)
image_name = doc.name_id
doc.status = 'Q'
doc.save()
image_processing.delay(uuid, image_name)
return HttpResponseRedirect(reverse('list_admin'))
tasks.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
import subprocess as sub
from PIL import Image
app = Celery('tasks',
broker='amqp://xxxxx',
backend='rpc://',)
NFS_PATH = '/home/administrator/nfs'
@app.task
def image_processing(uuid, image_name):
"""
WORKERS job: - Get the images from the NFS pre-processing path
- Move them to the worker path
- Process the images
- Create the PDF
- Leave the Results in the NFS post-processing path
"""
current_working_path = os.path.join(NFS_PATH, uuid)
local_filename_image = os.path.join(current_working_path, image_name)
local_filename_annotations = os.path.join(current_working_path, "annotations.json")
if os.path.isfile(local_filename_annotations):
local_filename_annotations = local_filename_annotations
else:
local_filename_annotations = "None"
cmd = '/home/administrator/Envs/CESSOR-env/bin/python' \
+ ' /home/administrator/CESSOR/compute.py' \
+ ' --iImage=' + local_filename_image \
print "Command:", cmd
p = sub.Popen(cmd.split(), stdout=sub.PIPE, stderr=sub.PIPE)
output, errors = p.communicate()
print output
# Get thumbnail of the uploaded image to render it in the detail.html template:
image_path = os.path.join(current_working_path, 'process_result.jpg')
img = Image.open(image_path)
w, h = img.size
a = 301.0
b = 200.0
ptgx = a/w
ptgy = b/h
w2 = w * ptgx
h2 = h * ptgy
w2 = int(w2)
h2 = int(h2)
img_thumbnail = img.resize((w2, h2), Image.ANTIALIAS)
# Save the thumbnail:
input_thumbnail_filename = 'process_result_thnl.png'
img_thumbnail.save(os.path.join(current_working_path, input_thumbnail_filename))
return uuid
私はpostgreSQLデータベースを使用しています。それは問題ですか? –
なぜそれは重要ですか?そのライブラリはあなたのDjango設定を使用します。 –
はい。私の答えを読んだ? –