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 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)
def application(env, start_response): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setblocking(0) c = s.connect_ex(('74.125.77.104', 80)) if c == errno.EINPROGRESS: uwsgi.green_wait_fdwrite(s.fileno(), 10) for r in send_request(env, s): yield r elif c == errno.EISCONN: for r in send_request(env, s): yield r else: uwsgi.green_schedule() start_response('500 Internal Server Error', [('Content-Type', 'text/html')]) yield "Internal Server Error" s.close()