import time from pprint import pprint from threading import Thread imp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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)