code: def gevent_wait_callback(conn, timeout=None):
      
      while 1:
          state = conn.poll()
          if state == extensions.POLL_OK:
              break
          elif state == extensions.POLL_READ:
              wait_read(conn.fileno(), timeout=timeout)
          elif state == extensions.POLL_WRITE:
              wait_write(conn.fileno(), timeout=timeout)
          else:
              raise OperationalError(
                  "Bad result from poll: %r" % state)
docs: A wait callback useful to allow gevent to work with Psycopg.