简体   繁体   English

RabbitMQ basic_publish是异步的吗?

[英]Is RabbitMQ basic_publish asynchronous?

I have the following Producer code. 我有以下生产者代码。

message is a dict data obtained after some computations. 消息是经过一些计算获得的字典数据。 I want to publish message to a queue. 我想将消息发布到队列中。 Then I reset message to an empty dict for some other calculations. 然后,我将消息重置为空字典,以进行其他一些计算。 But the consumer is always getting an empty dict from the queue. 但是消费者总是从队列中得到空洞的命令。 I feel message is reset before it is published(is it asynchronous?). 我觉得消息在发布之前已被重置(它是异步的吗?)。 How to make it synchronous ? 如何使其同步?

message = {a big dict ...}
channel.basic_publish(exchange='',
                            routing_key='my_queue',
                            body=json.dumps(message))
message = {}

Yes, channel.basic_publish is async, and it is correct in this way. 是的, channel.basic_publish是异步的,并且以这种方式是正确的。

channel.basic_publish does not wait the insert time, otherwise, It'd be slow. channel.basic_publish不等待插入时间,否则会很慢。

you should change the way how you handle your messages, but if you want to make is synchronous you can use the transactions. 您应该更改处理消息的方式, 但是如果要使消息同步,则可以使用事务。

should be something like: 应该是这样的:

channel.tx_select
channel.basic_publish(exchange='',
                            routing_key='my_queue',
                            body=json.dumps(message))
channel.tx_commit

Note transaction can drop the performances. 笔记交易会降低演奏性能。

Read here for more detail 在这里阅读更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM