Pika示例链接:https://github.com/pika/pika/blob/main/examples/basic_consumer_threaded.py

huangapple go评论118阅读模式
英文:

Pika example https://github.com/pika/pika/blob/main/examples/basic_consumer_threaded.py

问题

I have read some post in this group and I don’t understand what is the point of using:
https://github.com/pika/pika/blob/main/examples/basic_consumer_threaded.py

I mean, why we need one additional thread for processing message and calling add_callback_threadsafe where as argument we pass ack method (both activities in the same new thread for processing).
I wonder how we prevent form closing connection from channel with we consume message, because if processing is longer than heartbeat value we send ack afterwards and it is for me the same case like just consuming in the main thread and then sending ack.
What is the difference if we run processing and sending ack in separate one thread?

I am new in python and pika so sorry if this question is strange

I don't understand, I want explanation

英文:

I have read some post in this group and I don’t understand what is the point of using:
https://github.com/pika/pika/blob/main/examples/basic_consumer_threaded.py

I mean, why we need one additional thread for processing message and calling add_callback_threadsafe where as argument we pass ack method (both activities in the same new thread for processing).
I wonder how we prevent form closing connection from channel with we consume message, because if processing is longer than heartbeat value we send ack afterwards and it is for me the same case like just consuming in the main thread and then sending ack.
What is the difference if we run processing and sending ack in separate one thread?

I am new in python and pika so sorry if this question is strange

I don't understand, I want explanation

答案1

得分: 0

如果处理时间超过心跳值,然后发送确认消息,对我来说,这与在主线程中只消耗然后发送确认消息的情况是一样的。

不,那不对。如果在主线程中进行消耗,那么在发送确认消息之前处理消息所需的时间会阻塞 Pika 的 I/O 循环,因为 Pika 的 I/O 循环在创建连接的线程上运行(即主线程)。这就是为什么应该在单独的线程上执行工作的原因。

以下是一段示例代码的逐行解释:https://groups.google.com/g/rabbitmq-users/c/grHdO0s6Owo/m/F6ULK0G0BgAJ

我最近回答了不少关于“Pika如何与线程配合工作”的问题,它们源于对线程、Pika和其I/O循环的误解。Pika不会为I/O启动单独的线程。如果在创建Pika连接的线程中执行工作,将会阻塞I/O循环。

请注意,一般来说,进行I/O操作的程序或库都会在循环中执行,这会阻塞创建循环的线程。一些库会为此创建一个专用线程并将其“隐藏”在用户视线之外,但Pika不会这样做。

英文:

> if processing is longer than heartbeat value we send ack afterwards and it is for me the same case like just consuming in the main thread and then sending ack

Nope, that's not true. If you consume in the main thread, the time it takes to process the message prior to sending the ack BLOCKS Pika's I/O loop, because Pika's I/O loop runs on whatever thread created the connection (i.e. the main thread). This is why you should do work on a separate thread.

Here is a line-by-line explanation of some example code: https://groups.google.com/g/rabbitmq-users/c/grHdO0s6Owo/m/F6ULK0G0BgAJ

I've answered quite a few of these "how does Pika work with threads" questions lately and they all come down to a mis-understanding of threading, Pika and it's I/O loop. Pika does NOT start a separate thread for I/O. If you do work in the thread that you create the Pika connection on, you will block the I/O loop.

Note that, in general, any program or library that does I/O does so in a loop, which will block whatever thread on which the loop is created. Some libraries create a dedicated thread for this and "hide" it from the user, Pika does not.

huangapple
  • 本文由 发表于 2023年3月9日 19:17:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定