diff --git a/psycopg/connection.h b/psycopg/connection.h index f2296a9d..94d756dd 100644 --- a/psycopg/connection.h +++ b/psycopg/connection.h @@ -51,6 +51,7 @@ extern "C" { #define CONN_STATUS_GET_CLIENT_ENCODING 10 /* async query execution status */ +#define ASYNC_DONE 0 #define ASYNC_READ 1 #define ASYNC_WRITE 2 diff --git a/psycopg/connection_type.c b/psycopg/connection_type.c index ab5b23ed..8d68f71a 100644 --- a/psycopg/connection_type.c +++ b/psycopg/connection_type.c @@ -695,6 +695,7 @@ connection_setup(connectionObject *self, const char *dsn, long int async) self->status = async ? CONN_STATUS_SETUP : CONN_STATUS_READY; self->critical = NULL; self->async_cursor = NULL; + self->async_status = ASYNC_DONE; self->pgconn = NULL; self->mark = 0; self->string_types = PyDict_New(); diff --git a/psycopg/cursor_int.c b/psycopg/cursor_int.c index 11472843..32f3b72d 100644 --- a/psycopg/cursor_int.c +++ b/psycopg/cursor_int.c @@ -112,6 +112,7 @@ curs_poll_send(cursorObject *self) else if (res == 0) { /* all data flushed, start waiting for results */ Dprintf("cur_poll_send: returning %d", PSYCO_POLL_READ); + self->conn->async_status = ASYNC_READ; return PyInt_FromLong(PSYCO_POLL_READ); } else { @@ -153,6 +154,11 @@ curs_poll_fetch(cursorObject *self) if (last_result == 0) { Dprintf("cur_poll_fetch: returning %d", PSYCO_POLL_OK); + /* self->conn->async_status cannot be ASYNC_WRITE here, because we + never execute curs_poll_fetch in ASYNC_WRITE state, so we can + safely set it to ASYNC_DONE because we either fetched the result or + there is no result to fetch */ + self->conn->async_status = ASYNC_DONE; return PyInt_FromLong(PSYCO_POLL_OK); } else if (last_result == 1) { diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index 05d1436d..77b2393f 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -1491,7 +1491,10 @@ psyco_curs_poll(cursorObject *self) if (self->conn->async_status == ASYNC_WRITE) { return curs_poll_send(self); } - else { + else { + /* this gets called both for ASYNC_READ and ASYNC_DONE, because even + if the async query is complete, we still might want to check for + NOTIFYs */ return curs_poll_fetch(self); } }