pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adam Haberlach <adam@newsnipple.com>
To: pgsql-interfaces@postgresql.org
Subject: Asynchronous interface help?
Date: Tue, 2 Jan 2001 13:40:30 -0800
Message-ID: <20010102134030.A25130@newsnipple.com> (raw)

I'm working on using PostgreSQL to synchronize two seperate web connections
for some silly authentication reasons.  I'm adding a method to PHP that will
allow the generation of a page to wait on a named notify from another
backend process.  It doesn't seem to work correctly.  Here's the abridged
version of the code...

PHP_FUNCTION(pg_wait) {
    zval **timeout, **pgsql_link;
    int id = -1;
    fd_set rfds;
    int retval;
    PGconn *pgsql;
    PGnotify *notify;
    struct timeval tv;
    char *buf;
    int nbytes;
    int trycount=0;

	/* ... */

    ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    convert_to_long_ex(timeout);

    FD_ZERO(&rfds);
    FD_SET(PQsocket(pgsql), &rfds);

    tv.tv_sec = Z_STRVAL_PP(timeout);
    tv.tv_usec = 0;

    retval = select(1, &rfds, NULL, NULL, &tv);
    if(retval) {
        PQconsumeInput(pgsql);
        notify = PQnotifies(pgsql);
        if(notify) {
            nbytes = strlen(notify->relname)+1;
            buf = emalloc(nbytes);
            memcpy(buf, notify->relname, nbytes);
            free(notify);

		/* ... */

        }
    } else {
        printf("Timed out.  Socket==%d\n", PQsocket(pgsql));
        RETURN_FALSE;
    }

This always seems to time out.  Is there some mode that I need to put
the backend into in order to make the socket valid?  I'm getting a
non-stdio/stdout/stderr value back from PQsocket, so things can't be
too messed up.

The following code seems to work, but polls--we would much rather
block on select().

    while(trycount < Z_STRVAL_PP(timeout)) {
        PQconsumeInput(pgsql);
        notify = PQnotifies(pgsql);
        if(notify) {
            nbytes = strlen(notify->relname)+1;
            buf = emalloc(nbytes);
            memcpy(buf, notify->relname, nbytes);
            free(notify);

		/* ... */
        }
        sleep(1);
        trycount++;
    }


Anyone have any ideas?

-- 
Adam Haberlach            |A cat spends her life conflicted between a
adam@newsnipple.com       |deep, passionate, and profound desire for
http://www.newsnipple.com |fish and an equally deep, passionate, and
'88 EX500                 |profound desire to avoid getting wet.



view thread (3+ messages)  latest in thread

Message-ID: <20010102134030.A25130@newsnipple.com>
Permalink:  ../20010102134030.A25130@newsnipple.com/
Also on:    postgresql.org/message-id/20010102134030.A25130@newsnipple.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: adam@newsnipple.com
  Subject: Re: Asynchronous interface help?
  In-Reply-To: <20010102134030.A25130@newsnipple.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox