为什么`libpq`使用轮询而不是通知来获取数据? [英] Why does `libpq` use polling rather than notification for data fetch?

查看:76
本文介绍了为什么`libpq`使用轮询而不是通知来获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 libpq 参考。它同时具有sync和async方法。 Bu我发现了一些奇怪的东西。

I am reading libpq reference. It has both of sync and async methods. Bu I discovered something strange.

当我看到 PQsendQuery 函数时,它似乎发送查询并立即返回。我希望回调函数能够得到通知,但是没有这样的事情,该手册对 poll 说以确保数据可用性。

When I see PQsendQuery function, it seems to send a query and return immediately. And I expected a callback function to get notified, but there was no such thing and the manual says to poll for data availability.

我不明白为什么以轮询方式编写异步方法。无论如何,由于 libp 是官方的客户端实现,所以我相信应该有这种设计的充分理由。那是什么?还是我缺少其他地方提到的正确的回调函数?

I don't understand why async method is written in polling way. Anyway, as libp is the official client implementation, I believe there should be a good reason for this design. What is that? Or am I missing correct callback stuffs mentioned somewhere else?

推荐答案

在单线程程序的执行模型中,执行流程不能被异步查询(或更一般地说是网络套接字)返回的数据中断。只有信号( SIGTERM 和它的朋友)可以中断该流,但是信号不能被挂钩到传入的数据。

In the execution model of a mono-threaded program, the execution flow can't be interrupted by data coming back from an asynchronous query, or more generally a network socket. Only signals (SIGTERM and friends) may interrupt the flow, but signals can't be hooked to data coming in.

这就是为什么不可能有一个回调来通知传入数据的原因。如果您的代码未调用回调函数,那么libpq中的代码将无法运行。

That's why having a callback to get notified of incoming data is not possible. The piece of code in libpq that would be necessary to emit the callback would never run if your code doesn't call it. And if you have to call it, that defeats the whole point of a callback.

有些库如 Qt 提供回调,但它们是从头构建的,具有充当事件处理器的主循环。用户代码通过回调进行组织,并且可以对输入数据进行基于事件的处理。但是在这种情况下,库将拥有执行流的所有权,这意味着其主循环会轮询数据源。这只是将责任转移到libpq之外的另一段代码。

There are libraries like Qt that provide callbacks, but they're architectured from the ground up with a main loop that acts as an event processor. The user code is organized in callbacks and event-based processing of incoming data is possible. But in this case the library takes ownership of the execution flow, meaning its mainloop polls the data sources. That just shifts the responsibility to another piece of code outside of libpq.

这篇关于为什么`libpq`使用轮询而不是通知来获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆