psycogreen / tests /

#!/usr/bin/env python
"""A test to verify Psycopg collaboration with other blocking I/O.

Please run the script ``tools/`` in a separate shell to make the
test work.

If the test works you should see download tasks overlapping query tasks.

# Copyright (C) 2010-2012 Daniele Varrazzo <>
# All rights reserved.  See COPYING file for details.

import gevent
import gevent.monkey

import psycogreen.gevent

import urllib2  # green

import psycopg2

import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s")
logger = logging.getLogger()"testing psycopg2 with gevent")

conn = psycopg2.connect("dbname=postgres")

def download(num, secs):
    url = "http://localhost:8000/%d/" % secs
    for i in range(num):"download %d start", i)
        data = urllib2.urlopen(url).read()"download %d end", i)

def fetch(num, secs):
    cur = conn.cursor()
    for i in range(num):"query %d start", i)
        cur.execute("select pg_sleep(%s)", (secs,))"query %d end", i)"making jobs")
jobs = [
    gevent.spawn(download, 2, 3),
    gevent.spawn(fetch, 3, 2),
    ]"join begin")
gevent.joinall(jobs)"join end")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.