无法腌制:属性查找内建函数失败 [英] Can't pickle : attribute lookup builtin.function failed

查看:57
本文介绍了无法腌制:属性查找内建函数失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面收到错误消息,该错误仅在将 delay 添加到 process_upload 函数时才会发生,否则它将正常工作.

I'm getting the error below, the error only happens when I add delay to process_upload function, otherwise it works without a problem.

有人可以解释这个错误是什么,为什么会发生,并提供解决建议?

错误:

PicklingError at /contacts/upload/configurator/47/
  Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

这是视图

 if request.method == 'POST':
        form = ConfiguratorForm(data=request.POST)
        # Send import to task.
        process_upload.delay(upload_id=upload.id, form=form)

这是任务

@task
def process_upload(upload_id, form):
    upload = Upload.objects.get(id=upload_id)
    upload.process(form=form)

Upload.process在我的模型之内:

 def process(self, form):
        self.date_start_processing = timezone.now()
            import_this(data=self.filepath, extra_fields=[
                {'value': self.group_id, 'position': 5},
                {'value': self.uploaded_by.id, 'position': 6}], form=form)

完整跟踪:

Traceback:
File "/Users/user/Documents/workspace/sms/django-env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/user/Documents/workspace/sms/django-env/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  25.                 return view_func(request, *args, **kwargs)
File "/Users/user/Documents/workspace/sms/contacts/views.py" in upload_configurator
  118.         process_upload.delay(upload_id=upload.id, form=form)
File "/Users/user/Documents/workspace/sms/django-env/lib/python2.7/site-packages/celery/app/task.py" in delay
  357.         return self.apply_async(args, kwargs)
File "/Users/user/Documents/workspace/sms/django-env/lib/python2.7/site-packages/celery/app/task.py" in apply_async
  472.                                      **options)
File "/Users/user/Documents/workspace/sms/django-env/lib/python2.7/site-packages/celery/app/amqp.py" in publish_task
  249.             **kwargs
File "/Users/user/Documents/workspace/sms/django-env/lib/python2.7/site-packages/kombu/messaging.py" in publish
  157.             compression, headers)
File "/Users/user/Documents/workspace/sms/django-env/lib/python2.7/site-packages/kombu/messaging.py" in _prepare
  233.              body) = encode(body, serializer=serializer)
File "/Users/user/Documents/workspace/sms/django-env/lib/python2.7/site-packages/kombu/serialization.py" in encode
  161.         payload = encoder(data)
File "/Users/user/Documents/workspace/sms/django-env/lib/python2.7/site-packages/kombu/serialization.py" in dumps
  340.         return dumper(obj, protocol=pickle_protocol)

Exception Type: PicklingError at /contacts/upload/configurator/47/
Exception Value: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

forms.py

COL_CHOICES = [
    ('N/A', 'No Import'),
    ('first_name', 'First Name'),
    ('last_name', 'Last Name'),
    ('company', 'Company'),
    ('mobile', 'Mobile Number'),
    ('email', 'Email Address'),
    ]


class ConfiguratorForm(forms.Form):
    col1 = forms.TypedChoiceField(choices=COL_CHOICES, initial='first_name')
    col2 = forms.TypedChoiceField(choices=COL_CHOICES, initial='first_name')
    col3 = forms.TypedChoiceField(choices=COL_CHOICES, initial='first_name')
    col4 = forms.TypedChoiceField(choices=COL_CHOICES, initial='first_name')

推荐答案

您没有提供ConfiguratorForm的定义,但是无论如何:异步执行要求您任务的参数是可选取的,而显然您的表格则不是.您可能会艰难地选择它,但是那只是浪费时间.简单的解决方案是不传递表单,仅传递表单的数据(iow:request.POST.copy(),但我不确定Querydict是否可选)-或更好,首先验证表单并仅传递表单的cleaned_data,因为处理无效表格毫无意义.

You don't provide your ConfiguratorForm's definition but anyway: async execution requires that your task's arguments are pickable, and obviously your form is not. You could possibly go the hard way and make it pickable but that's just a waste of time. The simple solution is don't pass the form, only pass the form's data (iow: request.POST.copy() but I'm not sure Querydict are pickable) - or better, first validate the form and only pass the form's cleaned_data, as there's no point in processing an invalid form.

这篇关于无法腌制:属性查找内建函数失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆