In a single-threaded program execution model, the execution thread cannot be interrupted by data returned from an asynchronous request or, more generally, by a network socket. Only signals ( SIGTERM and friends) can interrupt the stream, but signals cannot connect to incoming data.
For this, it is not possible to receive a callback to receive notifications of incoming data. The piece of code in libpq that would be needed to fix the callback will never be executed unless your code calls it. And if you need to call it, this is detrimental to the whole callback.
There are libraries like Qt that provide callbacks, but they are based from scratch based on the main loop, which 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 responsibility for the flow of execution, that is, its mainloop of polling data sources. This simply transfers responsibility to another piece of code outside of libpq.
Daniel VΓ©ritΓ©
source share