Exemplo n.º 1
0
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
        yield data
    finally:
        uwsgi.close(fd)

    print "sleeping for 3 seconds..."
    uwsgi.async_sleep(3)
    uwsgi.suspend()
    yield "done"
Exemplo n.º 2
0
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
        yield data
    finally:
        uwsgi.close(fd)

    print("sleeping for 3 seconds...")
    uwsgi.async_sleep(3)
    uwsgi.suspend()
    yield "done"
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
def application(e, sr):
    sr('200 OK', [('Content-Type', 'text/plain')])

    # suspend 10 times and yield a value
    for i in range(1, 10):
        print i
        uwsgi.suspend()
        yield str(i)

    # 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"
Exemplo n.º 6
0
def application(env, start_response):

    c = uwsgi.async_connect('74.125.232.115:80')

    # wait for connection
    yield uwsgi.wait_fd_write(c, 2)

    if env['x-wsgiorg.fdevent.timeout']:
        uwsgi.close(c)
        raise StopIteration

    if uwsgi.is_connected(c):
        for r in send_request(env, c):
            yield r
    else:
        start_response('500 Internal Server Error', [('Content-Type', 'text/html')])
        yield "Internal Server Error"

    uwsgi.close(c)
Exemplo n.º 7
0
def application(env, start_response):

    c = uwsgi.async_connect('74.125.232.115:80')

    # wait for connection
    yield uwsgi.wait_fd_write(c, 2)

    if env['x-wsgiorg.fdevent.timeout']:
        uwsgi.close(c)
        raise StopIteration

    if uwsgi.is_connected(c):
        for r in send_request(env, c):
            yield r
    else:
        start_response('500 Internal Server Error',
                       [('Content-Type', 'text/html')])
        yield "Internal Server Error"

    uwsgi.close(c)
Exemplo n.º 8
0
def application(env, start_response):

    # open the socket
    fd = uwsgi.async_connect("192.168.173.100:3032")

    # wait for connection ready (3s timeout)
    yield uwsgi.wait_fd_write(fd, 3)

    # has timed out ?
    if env['x-wsgiorg.fdevent.timeout']:
        print "connection timed out !!!"
        uwsgi.close(fd)
        raise StopIteration

    # connection refused ?
    if not uwsgi.is_connected(fd):
        print "unable to connect"
        uwsgi.close(fd)
        raise StopIteration

    # send request
    # env can contains python objects, but send_message will discard them.
    # In this way we will automagically have a congruent and valid uwsgi packet
    uwsgi.async_send_message(fd, 0, 0, env)

    # send the http body
    # ready body in async mode and resend to fd
    # uwsgi.recv will use always an internal buffer of 4096, but can be limited in the number of bytes to read

    # does this request has a body ?
    cl = uwsgi.cl()

    if cl > 0:
        # get the input fd
        input = env['wsgi.input'].fileno()

        # read (in async mode) upto 'cl' data and send to uwsgi peer
        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)
            raise StopIteration
        data = uwsgi.recv(fd)

    uwsgi.close(fd)
Exemplo n.º 9
0
        raise Exception("unable to connect to %s" % addr)
    return fd
uwsgi.async_connect = uwsgi_pypy_async_connect

uwsgi.connection_fd = lambda: uwsgi_pypy_current_wsgi_req().fd

"""
uwsgi.wait_fd_read(fd, timeout=0)
"""
def uwsgi_pypy_wait_fd_read(fd, timeout=0):
    wsgi_req = uwsgi_pypy_current_wsgi_req();
    if lib.async_add_fd_read(wsgi_req, fd, timeout) < 0:
        raise Exception("unable to add fd %d to the event queue" % fd)
uwsgi.wait_fd_read = uwsgi_pypy_wait_fd_read

"""
uwsgi.wait_fd_write(fd, timeout=0)
"""
def uwsgi_pypy_wait_fd_write(fd, timeout=0):
    wsgi_req = uwsgi_pypy_current_wsgi_req();
    if lib.async_add_fd_write(wsgi_req, fd, timeout) < 0:
        raise Exception("unable to add fd %d to the event queue" % fd)
uwsgi.wait_fd_write = uwsgi_pypy_wait_fd_write

"""
uwsgi.ready_fd()
"""
def uwsgi_pypy_ready_fd():
    wsgi_req = uwsgi_pypy_current_wsgi_req();
    return lib.uwsgi_ready_fd(wsgi_req)
uwsgi.ready_fd = uwsgi_pypy_ready_fd
Exemplo n.º 10
0
        raise Exception("unable to connect to %s" % addr)
    return fd
uwsgi.async_connect = uwsgi_pypy_async_connect

uwsgi.connection_fd = lambda: uwsgi_pypy_current_wsgi_req().fd

"""
uwsgi.wait_fd_read(fd, timeout=0)
"""
def uwsgi_pypy_wait_fd_read(fd, timeout=0):
    wsgi_req = uwsgi_pypy_current_wsgi_req();
    if lib.async_add_fd_read(wsgi_req, fd, timeout) < 0:
        raise Exception("unable to add fd %d to the event queue" % fd)
uwsgi.wait_fd_read = uwsgi_pypy_wait_fd_read

"""
uwsgi.wait_fd_write(fd, timeout=0)
"""
def uwsgi_pypy_wait_fd_write(fd, timeout=0):
    wsgi_req = uwsgi_pypy_current_wsgi_req();
    if lib.async_add_fd_write(wsgi_req, fd, timeout) < 0:
        raise Exception("unable to add fd %d to the event queue" % fd)
uwsgi.wait_fd_write = uwsgi_pypy_wait_fd_write

"""
uwsgi.ready_fd()
"""
def uwsgi_pypy_ready_fd():
    wsgi_req = uwsgi_pypy_current_wsgi_req();
    return lib.uwsgi_ready_fd(wsgi_req)
uwsgi.ready_fd = uwsgi_pypy_ready_fd