txamqp-user team mailing list archive
-
txamqp-user team
-
Mailing list archive
-
Message #00013
Re: pickling objects through amqp
Hi Fabrizio
On Wed, Sep 2, 2009 at 1:01 PM, Fabrizio Mancini<mr.file@xxxxxxxxx> wrote:
> But on the consumer i receive only garbled strings that aren't
> converted back to normal object by pickle.
> Is this normal or am i missing something?
>
> This is the output:
> Received: S"(dp0\nS'a'\np1\nI123\nsS'b'\np2\nI456\ns."
> p0
> . from channel #1
This looks suspiciously like a pickled object. I think you're doing it
backwards, instead of dumping the object from the publisher and
loading it from the consumer.
That is, you recv_callback should look like this:
# txamqp_consumer.py
def recv_callback(msg, chan, queue):
msgbody = pickle.loads(msg.content.body)
print 'Received: ' + msgbody + ' from channel #' + str(chan.id)
return (queue.get().addCallback(recv_callback, chan, queue))
and you should publish your pickled objects like this:
# txamqp_publisher.py
msg = Content(pickle.dumps({'a':123,'b':456}))
In any case, I'd recommend not to use pickle for sending objects over
the network:
http://jcalderone.livejournal.com/15864.html
you may want to have a look at JSON which is fairly widespread, or at
Thrift which produces smaller messages than JSON and for which txAMQP
includes builtin support.
Cheers.
Follow ups
References