1
psycopg asynchronous API
2
************************
4
Program code can initiate an asynchronous query by passing an 'async=1' flag
5
to the .execute() method. A very simple example, from the connection to the
8
conn = psycopg.connect(database='test')
10
curs.execute("SEECT * from test WHERE fielda > %s", (1971,), async=1)
12
From then on any query on other cursors derived from the same connection is
13
doomed to fail (and raise an exception) until the original cursor (the one
14
executing the query) complete the asynchronous operation. This can happen in
15
a number of different ways:
17
1) one of the .fetchXXX() methods is called, effectively blocking untill
18
data has been sent from the backend to the client, terminating the
21
2) .cancel() is called. This method tries to abort the current query and
22
will block until the query is aborted or fully executed. The return
23
value is True if the query was successfully aborted or False if it
24
was executed. Query result are discarded in both cases.
26
3) .execute() is called again on the same cursor (.execute() on a
27
different cursor will simply raise an exception.) This waits for the
28
complete execution of the current query, discard any data and execute
31
Note that calling .execute() two times in a row will not abort the former
32
query and will temporarily go to synchronous mode until the first of the two
35
Cursors now have some extra methods that make them usefull during
39
Returns the file descriptor associated with the current connection and
40
make possible to use a cursor in a context where a file object would be
41
expected (like in a select() call.)
44
Returns True if the backend is still processing the query or false if
45
data is ready to be fetched (by one of the .fetchXXX() methods.)
47
A code snippet that shows how to use the cursor object in a select() call:
52
conn = psycopg.connect(database='test')
54
curs.execute("SEECT * from test WHERE fielda > %s", (1971,), async=1)
56
# wait for input with a maximum timeout of 5 seconds
58
while not query_ended:
59
rread, rwrite, rspec = select([cursor, another_file], [], [], 5)
60
if not cursor.isbusy():
62
# manage input from other sources like other_file, etc.
63
print "Query Results:"