def ugreen_wait_callback(conn, timeout=-1): """A wait callback useful to allow uWSGI/uGreen to work with Psycopg.""" while True: state = conn.poll() if state == psycopg2.extensions.POLL_OK: break elif state == psycopg2.extensions.POLL_READ: uwsgi.green_wait_fdread(conn.fileno()) elif state == psycopg2.extensions.POLL_WRITE: uwsgi.green_wait_fdwrite(conn.fileno()) else: raise Exception("Unexpected result from poll: %r", state)
def send_request(env, client): client.setblocking(1) client.send(b"GET /intl/it_it/images/logo.gif HTTP/1.0\r\n") client.send(b"Host: www.google.it\r\n\r\n") uwsgi.green_schedule() while 1: uwsgi.green_wait_fdread(client.fileno(), 10) buf = client.recv(4096) if len(buf) == 0: break else: yield buf
def async_wait(conn): # conn can be a connection or a cursor if not hasattr(conn, 'poll'): conn = conn.connection # interesting part: suspend until ready while True: state = conn.poll() if state == psycopg2.extensions.POLL_OK: break elif state == psycopg2.extensions.POLL_READ: uwsgi.green_wait_fdread(conn.fileno()) elif state == psycopg2.extensions.POLL_WRITE: uwsgi.green_wait_fdwrite(conn.fileno()) else: raise Exception("Unexpected result from poll: %r", state)