diff options
| author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2010-04-02 01:56:38 +0100 |
|---|---|---|
| committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2010-04-21 15:21:32 +0100 |
| commit | 1446f046e939db8359e1dcda682df1285872ad68 (patch) | |
| tree | 9a802994861acbd3f79d1c9a0461f5af159ffa29 /lib/extras.py | |
| parent | 02a28ff028b6f06625d2bb1dc4dadd639fbe3b43 (diff) | |
| download | psycopg2-1446f046e939db8359e1dcda682df1285872ad68.tar.gz | |
Added wait callback and functions to deal with it.
Diffstat (limited to 'lib/extras.py')
| -rw-r--r-- | lib/extras.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/extras.py b/lib/extras.py index 027c587..ac784b8 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -484,4 +484,28 @@ def register_tstz_w_secs(oids=None, conn_or_curs=None): return _ext.TSTZ_W_SECS +import select +from psycopg2.extensions import POLL_OK, POLL_READ, POLL_WRITE +from psycopg2 import OperationalError + +def wait_select(conn, curs=None): + """Wait until a connection or cursor has data available. + + The function is an example of a wait callback to be registered with + `~psycopg2.extensions.set_wait_callback()`. This function uses `!select()` + to wait for data available. + """ + poll = (curs or conn).poll + while 1: + state = poll() + if state == POLL_OK: + break + elif state == POLL_READ: + select.select([conn.fileno()], [], []) + elif state == POLL_WRITE: + select.select([], [conn.fileno()], []) + else: + raise OperationalError("bad state from poll: %s" % state) + + __all__ = filter(lambda k: not k.startswith('_'), locals().keys()) |
