Skip to content

Instantly share code, notes, and snippets.

@typesend
Created October 7, 2017 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typesend/0c63606cf7e8f11dab9f9cdd0352dac2 to your computer and use it in GitHub Desktop.
Save typesend/0c63606cf7e8f11dab9f9cdd0352dac2 to your computer and use it in GitHub Desktop.
Skeleton for naively pumping messages into and out of MQ using a simple loop.
# pip install redis
# pip install hiredis
# pip install python-dotenv
import os
from os.path import join, dirname
from dotenv import load_dotenv
import redis
import json
import time
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
REDIS_HOST = os.environ.get("REDIS_HOST") or 'localhost'
REDIS_PORT = os.environ.get("REDIS_PORT") or '6379'
REDIS_DB = os.environ.get("REDIS_DB") or '0'
r = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB)
def init():
r.flushdb()
return time.time()
def printCounter():
print(r.get('counter'))
def send():
r.incr('counter')
print("MQ <-- Message")
job = json.dumps({'timestamp': time.time(), 'message': r.get('counter')})
r.rpush('mq:in', job)
def receive():
r.incr('counter')
print("MQ --> Message")
job = json.dumps({'timestamp': time.time(), 'message': r.get('counter')})
r.rpush('mq:out', job)
try:
startTime = init()
while True:
send()
receive()
except KeyboardInterrupt, SystemExit:
print
print('Exiting...')
duration = int(time.time() - startTime)
messages_per_second = int(r.get('counter')) / duration
print(messages_per_second)
@typesend
Copy link
Author

typesend commented Oct 7, 2017

MQ --> Message
MQ <-- Message
MQ --> Message
MQ <-- Message
MQ --> Message
^C
Exiting...
3124

Without MQI involved I'm exceeding 2700 messages per second on my MacBook Air, which at least indicates a simple loop will suffice for now. I doubt we'll be processing anywhere near that many MQ messages. We can get fancy with threads later if the need for additional performance arises.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment