Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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.wait_fd_read(conn.fileno())
uwsgi.suspend()
elif state == psycopg2.extensions.POLL_WRITE:
uwsgi.wait_fd_write(conn.fileno())
uwsgi.suspend()
else:
raise Exception("Unexpected result from poll: %r", state)
def send_request(env, client):
uwsgi.send(client, b"GET /intl/it_it/images/logo.gif HTTP/1.0\r\n")
# test for suspend/resume
uwsgi.suspend()
uwsgi.send(client, b"Host: www.google.it\r\n\r\n")
while 1:
yield uwsgi.wait_fd_read(client, 2)
if env['x-wsgiorg.fdevent.timeout']:
return
buf = uwsgi.recv(client, 4096)
if buf:
yield buf
else:
break
# call suspend 10 times and yield some value
for i in range(0, 10):
print i
uwsgi.suspend()
yield str(i)
# connect to a memcached server
fd = uwsgi.async_connect('127.0.0.1:11211')
try:
# start waiting for socket availability (4 seconds max)
uwsgi.wait_fd_write(fd, 4)
# suspend execution 'til event
uwsgi.suspend()
uwsgi.send(fd, "get /foobar\r\n")
# now wait for memcached response
uwsgi.wait_fd_read(fd, 4)
uwsgi.suspend()
# read the response
data = uwsgi.recv(fd, 4096)
# return to the client
yield data
finally:
uwsgi.close(fd)
print "sleeping for 3 seconds..."
uwsgi.async_sleep(3)
uwsgi.suspend()
yield "done"
def recv(self, bufsize):
if not self.closed:
uwsgi.wait_fd_read(self.fileno(), 300)
uwsgi.suspend()
if not self.closed:
return uwsgi.recv(self.fileno(), bufsize)
while cl > 0:
bufsize = min(cl, 4096)
yield uwsgi.wait_fd_read(input, 30)
if env['x-wsgiorg.fdevent.timeout']:
print "connection timed out !!!"
uwsgi.close(fd)
raise StopIteration
body = uwsgi.recv(input, bufsize)
if body:
uwsgi.send(fd, body)
cl = cl - len(body)
else:
break
# wait for response (30s timeout)
yield uwsgi.wait_fd_read(fd, 30)
# has timed out ?
if env['x-wsgiorg.fdevent.timeout']:
print "connection timed out !!!"
uwsgi.close(fd)
raise StopIteration
data = uwsgi.recv(fd)
# recv the data, if it returns None the callable will end
while data:
yield data
# wait for response
yield uwsgi.wait_fd_read(fd, 30)
if env['x-wsgiorg.fdevent.timeout']:
print "connection timed out !!!"
uwsgi.close(fd)
# connect to a memcached server
fd = uwsgi.async_connect('127.0.0.1:11211')
try:
command = "get /foobar\r\n"
remains = len(command)
while remains > 0:
# start waiting for socket availability (4 seconds max)
uwsgi.wait_fd_write(fd, 4)
# suspend execution 'til event
uwsgi.suspend()
pos = len(command) - remains
written = uwsgi.send(fd, command[pos:])
remains -= written
# now wait for memcached response
uwsgi.wait_fd_read(fd, 4)
uwsgi.suspend()
# read a chunk of data
data = uwsgi.recv(fd, 4096)
# .. and yield it
yield data
finally:
# always ensure sockets are closed
uwsgi.close(fd)
print "sleeping for 3 seconds..."
uwsgi.async_sleep(3)
uwsgi.suspend()
yield "done"
def recv(self, bufsize):
if not self.closed:
uwsgi.wait_fd_read(self.fileno(), -1)
uwsgi.suspend()
if not self.closed:
return uwsgi.recv(self.fileno(), bufsize)