Why does libpq use polling rather than notification to retrieve data?

I am reading the libpq link. It has both synchronous and asynchronous methods. Boo, I discovered something strange.

When I see the PQsendQuery function, it seems to send a request and return immediately. And I expected the callback function to receive a notification, but this was not, and the manual says about conducting a survey on the availability of data.

I do not understand why the async method is written using the polling method. In any case, since libp is the official implementation of the client, I believe that there must be a good reason for this design. What is it? Or am I missing the right callback materials mentioned elsewhere?

+7
postgresql libpq
source share
2 answers

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.

+6
source share

This page describes how I can get notified for an asynchronous result.

http://www.postgresql.org/docs/9.3/static/libpq-events.html#LIBPQ-EVENTS-PROC

PGEVT_RESULTCREATE

A result creation event is fired in response to the execution of any query that generates a result, including PQgetResult. This event will not be triggered unless the result is successfully created.

typedef struct {PGconn * conn; Result PGresult *; } PGEventResultCreate; When the PGEVT_RESULTCREATE event is received, the evtInfo pointer should be discarded by PGEventResultCreate *. A compound is a compound used to generate a result. This is the perfect place to initialize any Data instance that needs to be associated with the result. If the event procedure failed, the result will be cleared and the failure will be propagated. the event procedure should not try PQclear for the result object for itself. When the failure code is returned, all cleanup should be performed as no The PGEVT_RESULTDESTROY event will be sent.

-one
source share

All Articles