easy to use redis(kb)
Project description
Easy to use python redis(for kb first)
Compared with kafka, redis has the following characteristics in publishing and subscribing:
Produced messages are immediately consumed by consumers.
Fast speed, small data( < 10KB).
Allow message loss.
No need to save sent messages.
Quick Start
Installation: pip install easy_redis
1.config
Edit conf/conf.yml
redis: # redis config server: 127.0.0.1 # redis server port: 6379 channel_subscribe: # channel if need, multiple - channel_1 - channel_2 channel_produce: channel_1_result # producer default channel
2.demo-consumer
import json from easy_redis.redis_consumer import EasyRedisConsumer def start_consumer(): redis_consumer = EasyRedisConsumer('../conf/conf.yml') print('consumer iterator started') for record in redis_consumer: if record['type'] == 'message': print('data', json.loads(record['data'])) else: print('subscribe', record) if __name__ == "__main__": start_consumer()
3.demo-consumer-task
import json from easy_redis.redis_consumer import EasyRedisConsumer from easy_redis.redis_producer import EasyRedisProducer redis_producer = EasyRedisProducer('../conf/conf.yml') def consumer_task(record): """ consumer callback :param record: object :return: """ print('consumer_task', ( 'received type: {}, channel: {}, msg: {}'.format(record['type'], record['channel'], record['data']))) if record['type'] == 'message': print('data', json.loads(record['data'])) if record['channel'] == 'channel_1': redis_producer.produce_msg({'type': 'task result'}) def start_consumer(): redis_consumer = EasyRedisConsumer('../conf/conf.yml') print('consumer task started') redis_consumer.subscribe(fn=consumer_task) if __name__ == "__main__": start_consumer()
4.demo-producer
from easy_redis.redis_config import EasyRedisConfig from easy_redis.redis_producer import EasyRedisProducer def demo_produce_msg(): redis_producer = EasyRedisProducer('../conf/conf.yml') redis_producer.produce_msg({'name': 'ksust'}) def demo_produce_msg_with_config(): config = EasyRedisConfig('../conf/conf.yml') print('config', config.__dict__) redis_producer = EasyRedisProducer(config) redis_producer.produce_msg({'name': 'ksust'}) redis_producer.produce_msg_channel('channel_1', {'name': 'ksust'}) if __name__ == "__main__": demo_produce_msg() demo_produce_msg_with_config()
5.demo-redis_conn
from easy_redis.redis_conn import EasyRedis def demo_redis(): redis_conn = EasyRedis('../conf/conf.yml').redis_conn redis_conn.set('demo', 'value') print(redis_conn.keys('*')) if __name__ == "__main__": demo_redis()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
easy_redis-0.0.4.tar.gz
(3.6 kB
view hashes)
Built Distribution
Close
Hashes for easy_redis-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e55cd144c13f463825d414bb68e0d6605d900f0e44405a513322778212bf8d1e |
|
MD5 | 57804544fd5011cebab3313f71cfe352 |
|
BLAKE2b-256 | 53a30a4ee67fb1f84c1ecb6db84cd57c65493d864544c4f6dda7d7c09569b261 |