Py学习  »  MQ

对rabbitmq使用sqlalchemy

Morgan Allen • 6 年前 • 1363 次点击  

我有一个运行在多个处理器上的webscraper,它可以写入sqlite数据库。由于sqlite不是用来处理并发写操作的,所以我想使用一个队列系统来处理多个输入,并且只向数据库中写入一个对象

我浏览了rabbitmq hello world示例,得到一个错误。

我的 send.py 文件:

from models import TestCan
from models import TestJobs

import pika

# init_db()

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

channel.basic_publish(exchange='',
                      routing_key='hello',
                      body=TestCan('Test'))

这个 models.py 用文件 TestCan :

class TestCan(Base):
    __tablename__ = 'test_can'
    id = Column(Integer, primary_key=True)
    name = Column(String())


    def __init__(self, name=None):
        self.name = name

    def __repr__(self):
        return '<Name: %s>' % (self.name)

我得到这个错误:

TypeError: object of type 'TestCan' has no len()

那是什么意思?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43305
文章 [ 1 ]  |  最新文章 6 年前
MatsLindh
Reply   •   1 楼
MatsLindh    7 年前

不能直接在rabbitmq上发布python对象,因为发布的消息是一个字符串。

必须先序列化和反序列化对象,方法是使用 json.dumps json.loads pickle 模块。

但是,如果您可以引入外部依赖项(例如rabbitmq),那么使用适当的rdbms是一个选项,或者最好完全避免使用任何外部工具—使您的父进程负责队列,并使您的子进程以某种方式将结果返回给父进程。