Created
October 7, 2017 08:10
-
-
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.
This file contains hidden or 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
# 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('Exiting...') | |
duration = int(time.time() - startTime) | |
messages_per_second = int(r.get('counter')) / duration | |
print(messages_per_second) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.