Created
January 9, 2014 13:46
-
-
Save squeaky-pl/8334318 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 | |
from gevent import pywsgi | |
from gevent.monkey import patch_all | |
from pymongo import MongoClient | |
patch_psycopg() | |
patch_all() | |
def main(): | |
job1 = gevent.spawn(greenloop, 'tcp://*:3333') | |
job2 = gevent.spawn(greenloop, 'tcp://*:3334') | |
job3 = gevent.spawn(greenpsycopg) | |
job4 = gevent.spawn(mongotail) | |
job5 = gevent.spawn(http) | |
gevent.signal( | |
signal.SIGTERM, gevent.killall, [job1, job2, job3, job4, job5]) | |
gevent.joinall([job1, job2, job3, job4, job5]) | |
def http(): | |
server = pywsgi.WSGIServer(('0.0.0.0', 8000), hello_http) | |
server.start() | |
def hello_http(env, start_response): | |
start_response('200 OK', [('Content-Type', 'text/plain')]) | |
yield 'Hello world\n' | |
gevent.sleep(2) | |
yield 'Wow\n' | |
gevent.sleep(1) | |
yield 'Sweet\n' | |
def mongotail(): | |
client = MongoClient() | |
capped = client.test.test | |
while True: | |
cur = capped.find(tailable=True, await_data=True) | |
messages = list(cur) | |
if messages: | |
print('MONGO', messages) | |
else: | |
gevent.sleep(.1) | |
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('POSTGRES', 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