You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3搭配Librabbitmq2.0.0出现TypeError:无法pickle memoryview对象

Fixing "TypeError: can't pickle memoryview objects" with librabbitmq 2.0.0 on Python 3.6

Hey there, I’ve run into similar Python 3 compatibility hiccups with older librabbitmq versions before, so let’s break down how to fix this pickle error you’re seeing.

Why This Happens

Librabbitmq 2.0.0’s master branch (as of your installation) has some leftover Python 2-era code that uses memoryview objects in places where Python 3’s pickle module can’t serialize them. Python 3 changed how pickle handles memoryviews, which leads to this exact error when the library tries to pass these objects through serialization—super common in Celery workflows, for example.

Step-by-Step Solutions

1. Check for a Pre-Existing Fix in the Repository

First, let’s see if the maintainers have already patched this issue:

  • Navigate to your cloned librabbitmq directory and fetch the latest updates:
    git fetch origin
    
  • Search the repo’s issues or pull requests for mentions of "pickle memoryview". If there’s a dedicated fix branch (like fix-python3-pickle or similar), check it out and reinstall:
    git checkout <name-of-fix-branch>
    make clean
    make install
    

2. Manually Patch the Code (If No Official Fix Exists)

If there’s no pre-made fix, you can tweak the relevant code to convert memoryview objects to bytes before serialization:

  • Use the error’s stack trace to find which source file is causing the issue (usually something like librabbitmq/amqp.py).
  • Look for lines that create or pass memoryview objects, like this:
    payload = memoryview(message_body)
    
  • Replace it with a bytes conversion to make pickle happy:
    payload = bytes(message_body)
    
  • Save the file, then reinstall the library to apply the change:
    make clean
    make install
    

3. Switch to a More Python 3-Compatible Version

If the 2.0.0 master branch is too buggy, consider moving to a later tag or release that explicitly supports Python 3:

  • List all available tags in the repo:
    git tag -l
    
  • Check out a tag like 2.0.1 (if it exists and fixes Python 3 issues):
    git checkout 2.0.1
    make clean
    make install
    
  • Alternatively, if a fixed version is on PyPI, install it directly with pip:
    pip install librabbitmq>=2.0.1
    

4. Temporary Workaround: Switch Serialization Backend (For Celery Users)

If you’re using librabbitmq with Celery, you can avoid the pickle issue entirely by using a different serializer like JSON:

  • Update your Celery configuration to use json instead of pickle:
    app = Celery('myapp')
    app.conf.update(
        task_serializer='json',
        accept_content=['json'],
        result_serializer='json',
    )
    

Note: This is a quick workaround—you’ll still want to fix the librabbitmq issue if you need pickle support for complex objects.


内容的提问来源于stack exchange,提问作者dowjones123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:54:56