Django RQ处理长串行器工作

时间:2017-05-03 04:39:21

标签: python django

我在模型中有验证器。它进行验证,然后验证的数据将保存到数据库中。

问题:
验证和提交数据库需要很长时间。
因此,我决定使用Django-rq来处理耗时的任务

views.py

def save_serializer(serializer, request):
    serializer.save()
    if bool(serializer.errors):
        # If it has no errors it will be empty dictionary and bool({}) is False
        msg = serializer.errors
    else:
        msg = serializer.data

    email = EmailMessage(
        'Message From jobs.siamsbrand.com',
        msg,
        settings.C0D1UM_SENDER,
        [request.user.email]
    )
    email.send()


class PriceListExcelFileList(generics.ListCreateAPIView):
    permission_classes = (DRYPermissions,)
    queryset = PriceListExcelFile.objects.all()
    serializer_class = PriceListExcelFileSerializer

    def perform_create(self, serializer, request):
        """
        :param serializer: 
        :return: 
        """
        django_rq.enqueue(save_serializer, serializer, request)

    def create(self, request, *args, **kwargs):
        """
        I need to override the default behaviour because I am going to use rq and let it be an email notification
        :param request: 
        :param args: 
        :param kwargs: 
        :return: 
        """
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer, request)
        headers = self.get_success_headers(serializer.data)
        data = {
            "id": "Processing",
            "file": "The response will be email to " + str(request.user.email) + " shortly",
            "permission": "-"
        }
        return Response(data, status=status.HTTP_201_CREATED, headers=headers)

错误讯息:

File "/Users/el/Code/siam-sbrand/portal/apps/price_list_excel_files/views.py", line 52, in create
    self.perform_create(serializer, request)
  File "/Users/el/Code/siam-sbrand/portal/apps/price_list_excel_files/views.py", line 40, in perform_create
    django_rq.enqueue(save_serializer, serializer, request)
  File "/Users/el/.pyenv/versions/siam-sbrand/lib/python3.6/site-packages/django_rq/queues.py", line 208, in enqueue
    return get_queue().enqueue(func, *args, **kwargs)
  File "/Users/el/.pyenv/versions/siam-sbrand/lib/python3.6/site-packages/rq/queue.py", line 274, in enqueue
    job_id=job_id, at_front=at_front, meta=meta)
  File "/Users/el/.pyenv/versions/siam-sbrand/lib/python3.6/site-packages/django_rq/queues.py", line 60, in enqueue_call
    return self.original_enqueue_call(*args, **kwargs)
  File "/Users/el/.pyenv/versions/siam-sbrand/lib/python3.6/site-packages/django_rq/queues.py", line 56, in original_enqueue_call
    return super(DjangoRQ, self).enqueue_call(*args, **kwargs)
  File "/Users/el/.pyenv/versions/siam-sbrand/lib/python3.6/site-packages/rq/queue.py", line 227, in enqueue_call
    job = self.enqueue_job(job, at_front=at_front)
  File "/Users/el/.pyenv/versions/siam-sbrand/lib/python3.6/site-packages/rq/queue.py", line 292, in enqueue_job
    job.save(pipeline=pipe)
  File "/Users/el/.pyenv/versions/siam-sbrand/lib/python3.6/site-packages/rq/job.py", line 465, in save
    connection.hmset(key, self.to_dict())
  File "/Users/el/.pyenv/versions/siam-sbrand/lib/python3.6/site-packages/rq/job.py", line 429, in to_dict
    obj['data'] = self.data
  File "/Users/el/.pyenv/versions/siam-sbrand/lib/python3.6/site-packages/rq/job.py", line 227, in data
    self._data = dumps(job_tuple)
TypeError: cannot serialize '_io.BufferedReader' object
"POST /api/price-list-excel-files/ HTTP/1.1" 500 20430

1 个答案:

答案 0 :(得分:0)

它可以处理普通函数而不是实例。然后我必须使用thread来完成耗时的任务