~onehundredthirty/+junk/uwsgi-exp

« back to all changes in this revision

Viewing changes to tests/psycopg2_green.py

  • Committer: Leonid Borisenko
  • Date: 2010-12-15 22:50:19 UTC
  • mfrom: (0.1.1 upstream)
  • Revision ID: leo.borisenko@gmail.com-20101215225019-490bw8i9jjvwnmzy
New uWSGI release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import uwsgi
 
2
import psycopg2
 
3
 
 
4
def async_wait(conn):
 
5
        # conn can be a connection or a cursor
 
6
        if not hasattr(conn, 'poll'):
 
7
                conn = conn.connection
 
8
        
 
9
        # interesting part: suspend until ready
 
10
        while True:
 
11
                state = conn.poll()
 
12
                if state == psycopg2.extensions.POLL_OK:
 
13
                        break
 
14
                elif state == psycopg2.extensions.POLL_READ:
 
15
                        uwsgi.green_wait_fdread(conn.fileno())
 
16
                elif state == psycopg2.extensions.POLL_WRITE:
 
17
                        uwsgi.green_wait_fdwrite(conn.fileno())
 
18
                else:
 
19
                        raise Exception("Unexpected result from poll: %r", state)
 
20
 
 
21
                        
 
22
 
 
23
 
 
24
 
 
25
def application(env, start_response):
 
26
 
 
27
        start_response('200 Ok', [('Content-type', 'text/html')])
 
28
 
 
29
        conn = psycopg2.connect("dbname=prova user=postgres", async=True)
 
30
 
 
31
        # suspend until connection
 
32
        async_wait(conn)
 
33
 
 
34
        curs = conn.cursor()
 
35
 
 
36
        yield "<table>"
 
37
 
 
38
        curs.execute("SELECT * FROM tests")
 
39
 
 
40
        # suspend until result
 
41
        async_wait(curs)
 
42
 
 
43
        while True:
 
44
                row = curs.fetchone()
 
45
                if not row: break
 
46
                yield "<tr><td>%s</td></tr>" % str(row)
 
47
 
 
48
        yield "</table>"
 
49
 
 
50
        conn.close()