英文:
How to use non-blocking connection function in libpq and trigger a notification or callback when the connection is established withlibpq — C Library?
问题
我正在尝试使用C中的libpq库建立与PostgreSQL数据库的非阻塞连接,并希望能够在连接建立后触发通知或回调函数。
我知道可以使用PQconnectStart()来启动非阻塞连接,并可以使用PQconnectPoll()来检查连接的状态,但我不确定如何在连接建立后触发通知或回调函数。
PGconn *conn;
PGconnStatusType status;
conn = PQconnectStart("dbname=mydatabase user=myusername password=mypassword");
if (conn == NULL) {
fprintf(stderr, "Connection initialization failed\n");
exit(1);
}
while ((status = PQconnectPoll(conn)) != CONNECTION_OK) {
if (status == PGRES_POLLING_FAILED) {
fprintf(stderr, "Connection failed: %s\n", PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}
}
// 连接已建立,如何触发通知或回调函数?
// 在此处处理连接...
PQfinish(conn);
请注意,代码中的通知或回调函数的触发部分不包括在内,您需要根据自己的需求添加适当的代码来执行所需的操作。
英文:
I am trying to establish a non-blocking connection to a PostgreSQL database using the libpq library in C, and I want to be able to trigger a notification or callback function once the connection is established.
I know that PQconnectStart() can be used to initiate a non-blocking connection and that PQconnectPoll() can be used to check the status of the connection, but I'm not sure how to trigger a notification or callback function once the connection is established.
PGconn *conn;
PGconnStatusType status;
conn = PQconnectStart("dbname=mydatabase user=myusername password=mypassword");
if (conn == NULL) {
fprintf(stderr, "Connection initialization failed\n");
exit(1);
}
while ((status = PQconnectPoll(conn)) != CONNECTION_OK) {
if (status == PGRES_POLLING_FAILED) {
fprintf(stderr, "Connection failed: %s\n", PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}
}
// Connection is established, how do I trigger a notification or callback function?
// Do something with the connection here...
PQfinish(conn);
答案1
得分: 0
我不认为你可以这样做。你需要在套接字上调用select()
来轮询是否有响应。你可以启动一个线程或并行进程来使用select()
进行轮询,然后调用你的回调函数。
英文:
I don't think you can do that. You'll have to call select()
on the socket to poll if there is a response. You can start a thread or parallel process that polls with select()
, the calls your callback.
答案2
得分: 0
我认为你可以使用通知接收器。
你可以使用PQsetNoticeReceiver来设置一个自定义的通知接收器回调函数,每当接收到通知或警告消息时都会调用该函数。这个回调函数可以用来根据你的应用程序的要求处理和处理接收到的消息。
英文:
I think that you can use the Notice Receiver.
You can use PQsetNoticeReceiver to set a custom notice receiver callback function that will be called whenever a notice or warning message is received. This callback function can be used to handle and process the received messages according to your application's requirements.
答案3
得分: 0
在连接建立后,触发通知会使用PQsetNoticeReceiver
函数。它允许您设置一个回调函数,每当从PostgreSQL接收到警告消息时都会调用该函数。
触发器应该如下所示:
PQsetNoticeReceiver(conn, notificationHandler, NULL);
其中notificationHandler
是已定义的回调函数。
英文:
Well, after the connection is established, triggering a notification will make use of the PQsetNoticeReceiver
function. It allows you to set a callback function that will be called whenever a warning message is received from PostgreSQL.
The trigger should be like so:
PQsetNoticeReceiver(conn, notificationHandler, NULL);
Where notificationHandler
is the defined callback function.
答案4
得分: 0
为了在连接建立后触发通知或回调函数,您可以使用PQsetNoticeReceiver函数来设置回调函数。当从数据库接收到通知时,它将被调用。
英文:
In order to trigger a notification or callback function once the connection is established, you can use the PQsetNoticeReceiver function to set a callback function. It will be invoked when a notice is received from the database.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论