Ejemplo 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"
Ejemplo 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"
Ejemplo n.º 3
0
def application(env, start_response):
    if env['REQUEST_METHOD'] == 'OPTIONS':
        content = b'Ok'
    else:
        uwsgi.async_sleep(1)
        content = b"Hello World"

    start_response('200 OK', [('Content-Type', 'text/plain'),
                              ('Content-Length', str(len(content)))])
    return [content]
Ejemplo n.º 4
0
    def process_response(self, request, response):
        # clusters - a list with varnish clusters to refresh
        clusters = VclRefreshState.get_refresh(request.id)
        if 'error_message' in request.session:
            del request.session['error_message']

        if len(clusters) > 0:
            start = time.perf_counter()
            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.error(
                    "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__, unescape_exception(e))

            logging.info("cluster reload time: %f" %
                         (time.perf_counter() - start))
        return response
Ejemplo 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"
Ejemplo n.º 6
0
def application(env, sr):

    if env['PATH_INFO'] == '/':
        uwsgi.websocket_handshake(env['HTTP_SEC_WEBSOCKET_KEY'], env.get('HTTP_ORIGIN', ''))
        describe = json.dumps({
            'describe': {
                'stream': {
                    'timestamp': {'label': "Server Timestamp", 'units': "s"}
                }
            }
        })
        uwsgi.websocket_send(describe.encode('utf-8'))
        while True:
            message = json.dumps({'timestamp': time.time()})
            uwsgi.websocket_send(message.encode('utf-8'))
            yield uwsgi.async_sleep(1)
Ejemplo n.º 7
0
            if lib.uwsgi.workers[i].sig:
                worker['status'] = 'sig%d' % lib.uwsgi.workers[i].signum
            elif lib.uwsgi_worker_is_busy(i):
                worker['status'] = 'busy' 
            else:
                worker['status'] = 'idle'
        worker['running_time'] = lib.uwsgi.workers[i].running_time
        worker['avg_rt'] = lib.uwsgi.workers[i].avg_response_time
        worker['tx'] = lib.uwsgi.workers[i].tx
            
        workers.append(worker)
    return workers
    
uwsgi.workers = uwsgi_pypy_workers

"""
uwsgi.async_sleep(timeout)
"""
def uwsgi_pypy_async_sleep(timeout):
    if timeout > 0:
        wsgi_req = uwsgi_pypy_current_wsgi_req();
        lib.async_add_timeout(wsgi_req, timeout);
uwsgi.async_sleep = uwsgi_pypy_async_sleep

"""
uwsgi.async_connect(addr)
"""
def uwsgi_pypy_async_connect(addr):
    fd = lib.uwsgi_connect(ffi.new('char[]', addr), 0, 1)
    if fd < 0:
        raise Exception("unable to connect to %s" % addr)
Ejemplo n.º 8
0
import uwsgi

sleepvalue = 5

def application(env, start_response):
        start_response('200 Ok', [('Content-type', 'text/html')])
    yield uwsgi.async_sleep(sleepvalue)
    #print "TIMEOUT: ", env['x-wsgiorg.fdevent.timeout']
    yield "<h1>Hello World after %d seconds</h1>" % sleepvalue
Ejemplo n.º 9
0
def application(e, s):
    s('200 OK', [('Content-Type', 'text/html')])
    for i in range(0, 3):
        yield uwsgi.async_sleep(1)
        yield "iter: %d<br/>" % i
Ejemplo n.º 10
0
def application(env, start_response):
    start_response('200 Ok', [('Content-type', 'text/html')])
    yield uwsgi.async_sleep(sleepvalue)
    # print("TIMEOUT: ", env['x-wsgiorg.fdevent.timeout'])
    yield "<h1>Hello World after %d seconds</h1>" % sleepvalue
Ejemplo n.º 11
0
            if lib.uwsgi.workers[i].sig:
                worker['status'] = 'sig%d' % lib.uwsgi.workers[i].signum
            elif lib.uwsgi_worker_is_busy(i):
                worker['status'] = 'busy' 
            else:
                worker['status'] = 'idle'
        worker['running_time'] = lib.uwsgi.workers[i].running_time
        worker['avg_rt'] = lib.uwsgi.workers[i].avg_response_time
        worker['tx'] = lib.uwsgi.workers[i].tx
            
        workers.append(worker)
    return workers
    
uwsgi.workers = uwsgi_pypy_workers

"""
uwsgi.async_sleep(timeout)
"""
def uwsgi_pypy_async_sleep(timeout):
    if timeout > 0:
        wsgi_req = uwsgi_pypy_current_wsgi_req();
        lib.async_add_timeout(wsgi_req, timeout);
uwsgi.async_sleep = uwsgi_pypy_async_sleep

"""
uwsgi.async_connect(addr)
"""
def uwsgi_pypy_async_connect(addr):
    fd = lib.uwsgi_connect(ffi.new('char[]', addr), 0, 1)
    if fd < 0:
        raise Exception("unable to connect to %s" % addr)
Ejemplo n.º 12
0
def application(env, start_response):
    start_response('200 Ok', [('Content-type', 'text/html')])
    yield uwsgi.async_sleep(sleepvalue)
    #print "TIMEOUT: ", env['x-wsgiorg.fdevent.timeout']
    yield "<h1>Hello World after %d seconds</h1>" % sleepvalue
Ejemplo n.º 13
0
def application(e, s):
    s('200 OK', [('Content-Type','text/html')])
    for i in range(0,3):
        yield uwsgi.async_sleep(1)
        yield "iter: %d<br/>" % i