-
Notifications
You must be signed in to change notification settings - Fork 19
/
examples.py
33 lines (25 loc) · 1.02 KB
/
examples.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
"""Example Rejected Consumer"""
import random
from rejected import consumer
from tornado import gen, httpclient
__version__ = '1.0.0'
class ExampleConsumer(consumer.SmartConsumer):
def process(self):
self.logger.info('Message: %r', self.body)
action = random.randint(0, 100)
self.stats_incr('action', action)
if action == 0:
raise consumer.ConsumerException('zomg')
elif action < 5:
raise consumer.MessageException('reject')
elif action < 10:
raise consumer.ProcessingException('publish')
class AsyncExampleConsumer(consumer.Consumer):
@gen.coroutine
def process(self):
self.logger.info('Message: %r', self.body)
http_client = httpclient.AsyncHTTPClient()
with self.stats_track_duration('async_fetch'):
results = yield [http_client.fetch('http://www.google.com'),
http_client.fetch('http://www.bing.com')]
self.logger.info('Length: %r', [len(r.body) for r in results])