publisher/subscriber implementations
--HG-- branch : sandbox
This commit is contained in:
58
src/subscriber/coop_eventlet.py
Normal file
58
src/subscriber/coop_eventlet.py
Normal file
@@ -0,0 +1,58 @@
|
||||
# Copyright (c) 2011 Andreas Balogh
|
||||
# See LICENSE for details.
|
||||
|
||||
""" prc_publish.eventlet
|
||||
|
||||
Price Publisher
|
||||
"""
|
||||
|
||||
# imports
|
||||
|
||||
from argparse import ArgumentParser
|
||||
import eventlet
|
||||
import logging
|
||||
import os
|
||||
from eventlet.green import socket
|
||||
import sys
|
||||
import cPickle as pickle
|
||||
|
||||
LOG = logging.getLogger()
|
||||
|
||||
# definitions
|
||||
|
||||
def main(argv=None):
|
||||
if argv is None:
|
||||
argv = sys.argv
|
||||
LOG.info("starting '%s %s'", os.path.basename(argv[0]), " ".join(argv[1:]))
|
||||
# parse options and arguments
|
||||
parser = ArgumentParser(description="Price Publisher")
|
||||
parser.add_argument("-f", "--file", dest="filename",
|
||||
help="read configuration from %(dest)s")
|
||||
parser.add_argument("-p", "--port", default=8001, type=int,
|
||||
help="server port [default: %(default)s")
|
||||
args = parser.parse_args()
|
||||
print args
|
||||
# application
|
||||
address = ('localhost', 8010)
|
||||
while True:
|
||||
subscriber(address)
|
||||
eventlet.sleep(1)
|
||||
|
||||
def subscriber(address):
|
||||
try:
|
||||
LOG.info("connecting to %s", address)
|
||||
cx = eventlet.connect(address)
|
||||
except socket.error, e:
|
||||
LOG.error("%i %s", e.errno, e)
|
||||
return
|
||||
while True:
|
||||
s = cx.recv(4096)
|
||||
item = pickle.loads(s)
|
||||
LOG.info("%s", item)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.basicConfig(level=logging.DEBUG,
|
||||
format='%(asctime)s.%(msecs)03i %(levelname).4s %(funcName)10s: %(message)s',
|
||||
datefmt='%H:%M:%S')
|
||||
main()
|
||||
Reference in New Issue
Block a user