import time
from pprint import pprint
from threading import Thread
import json
import itertools
from kombu import Connection, Consumer, Queue
from ioweb.stat import Stat
with open('var/config.json') as inp:
config = json.load(inp)
def thread_consumer(stat):
con = Connection(
'amqp://%s@localhost:5672//' % config['rabbitmq_credentials']
#prefetch_count=1
)
con.connect()
idx = itertools.count()
def func(body, msg):
#print('consumer: got msg')
#print('body', body, type(body))
#print('msg', msg, type(msg))
stat.inc('msg-rcv')
print('RCV #%d' % next(idx))
msg.ack()
with Consumer(con, [Queue('z')], callbacks=[func]):
con.drain_events(timeout=2)
def main(**kwargs):
stat = Stat(speed_keys=['msg-sent', 'msg-rcv'])
cons = Thread(target=thread_consumer, args=[stat])
cons.daemon = True
cons.start()
con = Connection(
'amqp://%s@localhost:5672//' % config['rabbitmq_credentials']
)
con.connect()
prod = con.Producer()
while True:
res = prod.publish(
{'time': str(time.time())},
exchange='',
routing_key='z',
)
#print('Sent msg to queue')
#stat.inc('msg-sent')
time.sleep(1)