1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#!/usr/bin/env python
"""A test to verify Psycopg collaboration with other blocking I/O.
Please run the script ``wait_server.py`` in a separate shell to make the
test work.
If the test works you should see download tasks overlapping query tasks.
"""
# Copyright (C) 2010-2018 Daniele Varrazzo <daniele.varrazzo@gmail.com>
# All rights reserved. See COPYING file for details.
import eventlet
eventlet.monkey_patch() # noqa
import psycogreen.eventlet
psycogreen.eventlet.patch_psycopg() # noqa
from six.moves.urllib.request import urlopen # green
import psycopg2
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s")
logger = logging.getLogger()
logger.info("testing psycopg2 with eventlet")
conn = psycopg2.connect("dbname=postgres")
def download(num, secs):
url = "http://localhost:8000/%d/" % secs
for i in range(num):
logger.info("download %d start", i)
urlopen(url).read()
logger.info("download %d end", i)
def fetch(num, secs):
cur = conn.cursor()
for i in range(num):
logger.info("query %d start", i)
cur.execute("select pg_sleep(%s)", (secs,))
logger.info("query %d end", i)
logger.info("making jobs")
pool = eventlet.GreenPool()
pool.spawn(download, 2, 3),
pool.spawn(fetch, 3, 2),
logger.info("join begin")
pool.waitall()
logger.info("join end")
|