Thread: How to use PQconsumeInput and PQisBusy correctly ?

How to use PQconsumeInput and PQisBusy correctly ?

From
"Rodrigo Moreno"
Date:
Hi all,

What's the right way to use pqconsumeinput and pqisbusy ?

The below code could be used to do that ? 

Thanks a Lot
Rodrigo Moreno


PS: Please don't worry if some code is wrong, I do not test this code yeat.


void query(PGconn *conn, const char *stmt)
{   PGresult    *res;   int         nFields;   int         i,j;   int         send;
   send = PQsendQuery(conn, stmt);      if (PQconsumeInput(conn))   {       while (PQisBusy(conn))       {
printf(".");          sleep(500);           PQconsumeInput(conn);       }              res = PQgetResult(conn);
                    if (PQresultStatus(res) != PGRES_TUPLES_OK)       {           fprintf(stderr, "failed: %s",
PQerrorMessage(conn));          PQclear(res);           exit_nicely(conn);       }              nFields =
PQnfields(res);             for (i = 0; i < nFields; i++)           printf("%-15s", PQfname(res, i));
  printf("\n\n");              for (i = 0; i < PQntuples(res); i++)       {           for (j = 0; j < nFields; j++)
         printf("%-15s", PQgetvalue(res, i, j));                          printf("\n");       }
PQclear(res);  }        
 
}




Re: How to use PQconsumeInput and PQisBusy correctly ?

From
Tom Lane
Date:
"Rodrigo Moreno" <rodrigo.miguel@terra.com.br> writes:
> What's the right way to use pqconsumeinput and pqisbusy ?
> The below code could be used to do that ? 

It would work but it seems rather pointless; the only reason you would
use those functions is if you have something else useful to do while
waiting for the query result ... and this program structure isn't
very conducive to doing other stuff while waiting.  Usually you'd want
to have a main loop that can dispatch handling of various events, one
of them being checking for an available query result.

The whole thing is a bit of a stone-age technique anyway; if you were
thinking of starting a new program today you'd probably use threads in
preference to an event loop.
        regards, tom lane


RES: How to use PQconsumeInput and PQisBusy correctly ?

From
"Rodrigo Moreno"
Date:
Hi Tom
> It would work but it seems rather pointless; the only reason you would use
those functions is if you have something else > useful to do while waiting
for the query result ... and this program structure isn't very conducive to
doing other stuff > while waiting.  Usually you'd want to have a main loop
that can dispatch handling of various events, one of them being >  >
checking for an available query result.

This is only a sample to demonstrante that I want. But I have plans to do
something, in fact, it will be a win32 application , that shows a non modal
Dialog with a Cancel button and a progessbar. If user wanna cancel the
query, just press cancel button. So, I don't want allows the user to do
anything else, they can't navigate trough a program, because this will
generate another query to be submited to pg, and I don't have plans to
create another one connection for this user. So, they will wait for query to
be finish or then press cancel, that is it.

But, my question, I always should call pqconsume before a pqisbusy ? Can I
use it in a loop ? It will cause some kind of traffic ?

Best Regards
Rodrigo Moreno





Re: RES: How to use PQconsumeInput and PQisBusy correctly ?

From
Tom Lane
Date:
"Rodrigo Moreno" <rodrigo.miguel@terra.com.br> writes:
> But, my question, I always should call pqconsume before a pqisbusy ?

Up to you.  You can always call either one safely, AFAIR.  The isbusy
result tells you whether you can call pqgetresult without blocking.
isbusy itself doesn't change any state --- to make something happen
(ie, collect data from the socket) you need to call consumeinput.
        regards, tom lane


RES: RES: How to use PQconsumeInput and PQisBusy correctly ?

From
"Rodrigo Moreno"
Date:
Thanks Tom,

One more question, today I'm using the syncronous functions, like PQexec,
so, my apps is frozen until the query to be finish.

I'm wondering

1) Use pqexec in a separate thread, wait the thread or call
PQgetCancel/PQcancel when user request that, or

2) Use PQconsumeInput/PQisBusy, in a non-thread program and wait for query
to be finish or user request their cancel.

What is the better one ?

Thanks 
Rodrigo Moreno