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
def application(e, sr):
sr('200 OK', [('Content-Type', 'text/plain')])
# 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
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"
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"
try:
result = load_vcl_task.delay(
timezone.now(),
[cluster.id for cluster in clusters]
)
if 'tastypie' in str(type(response)) and 'respond-async' in request.META.get('HTTP_PREFER', ''):
response.status_code = 202
response['Location'] = '/api/v0.1/task/{}/'.format(result.id)
else:
if settings.ENABLE_UWSGI_SWITCH_CONTEXT:
try:
import uwsgi
while not result.ready():
uwsgi.async_sleep(1)
uwsgi.suspend()
except:
pass
result.get()
if isinstance(result.result, Exception):
raise result.result
except SoftTimeLimitExceeded:
logging.info("Time for finish the task has been reached: The task with id {} will be killed.".format(
result.id))
except Exception as e:
logging.info("Error while reloading cluster: %s (%s)" % (e, type(response)))
if 'tastypie' in str(type(response)):
return HttpApplicationError("%s: %s" % (e.__class__.__name__, str(e)[:400]))
request.session['error_message'] = "%s: %s" % (e.__class__.__name__, str(e))
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)
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)