-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
44 lines (34 loc) · 1.16 KB
/
application.py
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
import time
import logging
logger = logging.getLogger(__name__)
class Producer(object):
def __init__(self, id, n, client):
self.id = id
self.n = n
self.client = client
self.total_sent = 0
def run(self):
print 'Started producer %s' % self.id
for i in xrange(self.n):
try:
self.client.publish('%s:%s' % (self.id, i))
except KeyboardInterrupt:
return
self.total_sent += 1
print 'Producer %s\tsent "%s:%s"\ttotal_sent %s' % (self.id, self.id, i, self.total_sent)
class Consumer(object):
def __init__(self, id, sleep, client):
self.id = id
self.sleep = sleep
self.client = client
self.total_received = 0
def run(self):
print 'Started consumer %s' % self.id
try:
self.client.consume(self.callback)
except KeyboardInterrupt:
return
def callback(self, message, subject=None):
self.total_received += 1
print 'Consumer %s\treceived "%s"\ttopic %s\ttotal_received %s' % (self.id, message, subject, self.total_received)
time.sleep(self.sleep)