From: Murthy K. <mur...@go...> - 2002-12-13 15:27:43
|
Gerhard, thanks. I think I can use this for what I need. At a more fundamental level though, if you submit a synchronous query through = libpq, and then kill the process from which the connection was opened/query = was submitted, shouldn't libpq assume and implicit requestCancel, and clean = up? I'll ask on pgsql-admin admin also; there was a recent thread on = canceling queries which I'll pick up. Thanks again, Murthy -----Original Message----- From: Gerhard H=E4ring [mailto:ger...@op...] Sent: Friday, December 13, 2002 06:37 To: pyp...@li... Subject: [Pypgsql-users] Re: How to stop query execution from Python/pyPgS QL Billy G. Allie <bga=3DN...@pu...> wrote: > [cancelling a query] > Since PgSQL is built upon the libpq module, you can access the > PGrequestCancel function as follows (assuming 'cx' is the connection > running the request you wish to cancel): >=20 > cx.conn.requestCancel() >=20 > This would have to be issued by a different thread of execution or = from a > signal handler since the execute method does not return until the = query > has finished processing at the backend. Can this work? I was under the impression that this was one of the = functions that's supposed to be used under the asynchronous API. Incidentally, I've tried to use the async functions in pyPgSQL.libpq a = few days back (and cancelling a query), but didn't succeed. Today, I've = found out why and committed a change to pgconnection.c. I believe you'll have = to use the CVS version of pyPgSQL to cancel queries for now. Here's a toy example with which I tested this functionality: #v+ # Experiment with asynchronous query processing and cancelling queries. from pyPgSQL import libpq import select conn =3D libpq.PQconnectdb("host=3Dmyhost dbname=3Dmydb") class BreakOut(Exception): pass counter =3D 0 for i in range(10): try: print "-" * 50 print "Starting query." conn.sendQuery("SELECT NAME FROM TEST") # 100000 rows while True: counter +=3D 1 if counter =3D=3D 300: raise BreakOut fds =3D select.select([conn.socket], [], []) if len(fds) !=3D 0: conn.consumeInput() if not conn.isBusy: break res =3D conn.getResult() # here we have the entire resultset res =3D conn.getResult() assert res =3D=3D None print "Query successfully processed." except BreakOut: # Cancel currently running query. print "Cancelling query ..." conn.requestCancel() while True: fds =3D select.select([conn.socket], [], []) if len(fds) !=3D 0: conn.consumeInput() if not conn.isBusy: break try: res =3D conn.getResult() except libpq.OperationalError, e: assert str(e).strip() =3D=3D "ERROR: Query was cancelled." res =3D conn.getResult() assert res =3D=3D None print "Query sucessfully cancelled." conn.finish() #v- ciao, Gerhard --=20 Gerhard H=E4ring OPUS GmbH M=FCnchen Tel.: +49 89 - 889 49 7 - 32 http://www.opus-gmbh.net/ ------------------------------------------------------- This sf.net email is sponsored by: With Great Power, Comes Great Responsibility=20 Learn to use your power at OSDN's High Performance Computing Channel http://hpc.devchannel.org/ _______________________________________________ Pypgsql-users mailing list Pyp...@li... https://lists.sourceforge.net/lists/listinfo/pypgsql-users |