summaryrefslogtreecommitdiff
path: root/sandbox
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2012-10-06 11:58:52 +0100
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2012-10-09 02:01:29 +0100
commit58d048198f88b882ce7530b5eb2dec672c36c8a0 (patch)
treec16235ce51ddd9c5e39c6c27b4ec69b4f8f17b82 /sandbox
parent7632e1ae46d0f65b6302f02d0b6a5c236b84c0fe (diff)
downloadpsycopg2-58d048198f88b882ce7530b5eb2dec672c36c8a0.tar.gz
Close the connection on error in callback
Unfortunately PQcancel blocks, so it's not better than PQgetResult. It has been suggested to use PQreset in non-blocking way but this would give the Python program the burden of handling a connection done but not configured in an unexpected place.
Diffstat (limited to 'sandbox')
-rw-r--r--sandbox/test_green_error.py47
1 files changed, 36 insertions, 11 deletions
diff --git a/sandbox/test_green_error.py b/sandbox/test_green_error.py
index 7d17cf7..7477382 100644
--- a/sandbox/test_green_error.py
+++ b/sandbox/test_green_error.py
@@ -4,15 +4,20 @@
DSN = 'dbname=test'
-# import eventlet.patcher
-# eventlet.patcher.monkey_patch()
+import eventlet.patcher
+eventlet.patcher.monkey_patch()
import os
import signal
+from time import sleep
+
import psycopg2
from psycopg2 import extensions
from eventlet.hubs import trampoline
+
+# register a test wait callback that fails if SIGHUP is received
+
panic = []
def wait_cb(conn):
@@ -34,23 +39,43 @@ def wait_cb(conn):
extensions.set_wait_callback(wait_cb)
+
+# SIGHUP handler to inject a fail in the callback
+
def handler(signum, frame):
panic.append(True)
signal.signal(signal.SIGHUP, handler)
+
+# Simulate another green thread working
+
+def worker():
+ while 1:
+ print "I'm working"
+ sleep(1)
+
+eventlet.spawn(worker)
+
+
+# You can unplug the network cable etc. here.
+# Kill -HUP will raise an exception in the callback.
+
+print "PID", os.getpid()
conn = psycopg2.connect(DSN)
curs = conn.cursor()
-print "PID", os.getpid()
try:
- curs.execute("select pg_sleep(1000)")
+ for i in range(1000):
+ curs.execute("select %s, pg_sleep(1)", (i,))
+ r = curs.fetchone()
+ print "selected", r
+
except BaseException, e:
print "got exception:", e.__class__.__name__, e
-conn.rollback()
-curs.execute("select 1")
-print curs.fetchone()
-
-
-# You can unplug the network cable etc. here.
-# Kill -HUP will raise an exception in the callback.
+if conn.closed:
+ print "the connection is closed"
+else:
+ conn.rollback()
+ curs.execute("select 1")
+ print curs.fetchone()