1"""
2safer test for continulet with partial writes check
3
4to enable continulets you only need to call uwsgi_pypy_setup_continulets() soon after startup:
5
6uwsgi --pypy-wsgi-file t/pypy/t_continulet2.py --http-socket :9090 --pypy-home /opt/pypy --pypy-eval "uwsgi_pypy_setup_continulets()" --async 8
7
8"""
9import uwsgi
10def application(e, sr):
11    sr('200 OK', [('Content-Type','text/plain')])
12
13    # suspend 10 times and yield a value
14    for i in range(1,10):
15        print i
16        uwsgi.suspend()
17        yield str(i)
18
19    # connect to a memcached server
20    fd = uwsgi.async_connect('127.0.0.1:11211')
21    try:
22        command = "get /foobar\r\n"
23        remains = len(command)
24        while remains > 0:
25            # start waiting for socket availability (4 seconds max)
26            uwsgi.wait_fd_write(fd, 4)
27            # suspend execution 'til event
28            uwsgi.suspend()
29            pos = len(command) - remains
30            written = uwsgi.send(fd, command[pos:])
31            remains -= written
32
33        # now wait for memcached response
34        uwsgi.wait_fd_read(fd, 4)
35        uwsgi.suspend()
36        # read a chunk of data
37        data = uwsgi.recv(fd, 4096)
38        # .. and yield it
39        yield data
40    finally:
41        # always ensure sockets are closed
42        uwsgi.close(fd)
43
44    print "sleeping for 3 seconds..."
45    uwsgi.async_sleep(3)
46    uwsgi.suspend()
47    yield "done"
48