我希望在我的linux項目中異步使用PostgreSQL(9.1)。爲此,我必須使用epoll_wait(因爲應用程序的其他部分)。目標是最終在邊緣觸發模式下使用epoll。但是我無法使連接過程起作用,即使在非邊緣觸發模式下也是如此。我不知道爲什麼。但是,當用戶名和密碼正確時,它就可以工作。但是,當密碼錯誤時,它也必須工作。在這種情況下,我收到了一些我不明白的錯誤。 : - /這是我使用(連接已經用完PQconnectStart(),並與PQconnectdb里正常工作的參數列表()初始化)代碼:PostgreSQL與epoll_wait異步連接
void ConnectDB(PGconn * connection)
{
int pq_fd = PQsocket(connection);
int epoll_fd = epoll_create1(0);
struct epoll_event event;
struct epoll_event *eventList = (epoll_event *)calloc(64, sizeof(epoll_event));
event.data.fd = pq_fd;
event.events = EPOLLOUT | EPOLLERR;
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, pq_fd, &event);
while (true) {
PostgresPollingStatusType pt = PQconnectPoll(connection);
switch (pt)
{
case PGRES_POLLING_OK:
printf("*** connection established!\n");
return;
case PGRES_POLLING_FAILED:
printf("*** connection failed: %s\n", PQerrorMessage(connection));
return;
case PGRES_POLLING_ACTIVE:
printf(" --- poll result: PGRES_POLLING_ACTIVE\n");
break;
case PGRES_POLLING_READING:
printf(" --- poll result: PGRES_POLLING_READING - Modifiing epoll to EPOLLIN\n");
event.events = EPOLLIN | EPOLLERR;
if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, PQsocket(connection), &event) == -1) {
printf("epoll_ctl() error: %u: %s\n", errno, strerror(errno));
exit(1);
}
break;
case PGRES_POLLING_WRITING:
printf(" --- poll result: PGRES_POLLING_WRITING - Modifiing epoll to EPOLLOUT\n");
event.events = EPOLLOUT | EPOLLERR;
if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, PQsocket(connection), &event) == -1) {
printf("epoll_ctl() error: %u: %s\n", errno, strerror(errno));
exit(1);
}
break;
}
int n = epoll_wait(epoll_fd, eventList, 64, -1);
if (n == -1) {
printf("epoll_wait() error: %u: %s\n", errno, strerror(errno));
exit(1);
}
}
}
這裏是輸出我得到:
--- poll result: PGRES_POLLING_WRITING - Modifiing epoll to EPOLLOUT
--- poll result: PGRES_POLLING_READING - Modifiing epoll to EPOLLIN
--- poll result: PGRES_POLLING_READING - Modifiing epoll to EPOLLIN
--- poll result: PGRES_POLLING_READING - Modifiing epoll to EPOLLIN
--- poll result: PGRES_POLLING_WRITING - Modifiing epoll to EPOLLOUT
--- poll result: PGRES_POLLING_READING - Modifiing epoll to EPOLLIN
--- poll result: PGRES_POLLING_READING - Modifiing epoll to EPOLLIN
--- poll result: PGRES_POLLING_WRITING - Modifiing epoll to EPOLLOUT
epoll_ctl() error: 2: No such file or directory
有沒有人有想法?
這有幫助 - 非常感謝! – SchodMC