2

我正在使用django-rqdjango 绑定python-rq来尝试异步生成 PDF。该类TemplateProcesser使用两个参数进行初始化,并在函数中自动生成 PDF __init__。这可以在 之外同步正常工作django-rq,但是django-rq会因以下错误而失败:

错误:

AttributeError: type object 'TemplateProcesser' has no attribute 'rsplit'

从这个电话:

django_rq.enqueue(TemplateProcesser, nail_order=serializer.object, user_photo=base64_image)

关于如何正确包含未实例化的类的任何想法django-rq

班级:

class TemplateProcesser(object):

    def __init__(self, nail_order, user_photo, *args, **kwargs):
        self.nail_order = nail_order
        self.user_photo = user_photo
        ...
        self.procces_template()
    ...

堆栈跟踪:

Traceback (most recent call last):
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/rq/worker.py", line 426, in perform_job
    rv = job.perform()
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/rq/job.py", line 386, in perform
    self._result = self.func(*self.args, **self.kwargs)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/rq/job.py", line 154, in func
    return import_attribute(self.func_name)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/rq/utils.py", line 168, in import_attribute
    module_name, attribute = name.rsplit('.', 1)
AttributeError: type object 'TemplateProcesser' has no attribute 'rsplit'

超孔径回答后的回溯:

Traceback (most recent call last):
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/rest_framework/views.py", line 400, in dispatch
    response = self.handle_exception(exc)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/rest_framework/views.py", line 397, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/admin/dev/ncla-web/ncla/api/views.py", line 91, in post
    django_rq.enqueue(self.template_processor_factory, **parameter_dict)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django_rq/queues.py", line 162, in enqueue
    return get_queue().enqueue(func, *args, **kwargs)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/rq/queue.py", line 213, in enqueue
    description=description, depends_on=depends_on)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django_rq/queues.py", line 42, in enqueue_call
    return self.original_enqueue_call(*args, **kwargs)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django_rq/queues.py", line 37, in original_enqueue_call
    return super(DjangoRQ, self).enqueue_call(*args, **kwargs)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/rq/queue.py", line 176, in enqueue_call
    return self.enqueue_job(job)
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/rq/queue.py", line 232, in enqueue_job
    job.save()
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/rq/job.py", line 360, in save
    connection.hmset(key, self.dump())
  File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/rq/job.py", line 329, in dump
    obj['data'] = dumps(self.job_tuple)
  File "/Users/admin/dev/ncla-web/env/bin/../lib/python2.7/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle BytesIO objects
4

1 回答 1

0

看起来python-rq 需要一个函数作为第一个参数。我会尝试:

def template_processor_factory(*args, **kwargs):
    return TemplateProcessor(*args, **kwargs)

django_rq.enqueue(template_processer_factory, nail_order=serializer.object,
                  user_photo=base64_image)
于 2014-05-28T22:33:02.443 回答