Created
January 7, 2014 17:11
-
-
Save squeaky-pl/8302674 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import signal | |
import gevent | |
import zmq.green as zmq | |
import psycopg2 | |
from psycogreen.gevent import patch_psycopg | |
patch_psycopg() | |
def main(): | |
job1 = gevent.spawn(greenloop, 'tcp://*:3333') | |
job2 = gevent.spawn(greenloop, 'tcp://*:3334') | |
job3 = gevent.spawn(greenpsycopg) | |
gevent.signal(signal.SIGTERM, gevent.killall, [job1, job2, job3]) | |
gevent.joinall([job1, job2, job3]) | |
def greenpsycopg(): | |
connection = psycopg2.connect( | |
database='test', user='postgres', password='postgres', | |
host='localhost', port=5433) | |
cur = connection.cursor() | |
while True: | |
cur.execute('SELECT pg_sleep(5), 1 as r') | |
print(cur.fetchall()) | |
def greenloop(address): | |
ctx = zmq.Context() | |
socket = ctx.socket(zmq.PULL) | |
socket.bind(address) | |
while True: | |
try: | |
msg = socket.recv() | |
except gevent.GreenletExit: | |
print('EXIT', address) | |
return | |
print(address, msg) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment