No project description provided
Project description
Muffin-Redis – Redis support for Muffin framework.
Requirements
python >= 3.3
Installation
Muffin-Redis should be installed using pip:
pip install muffin-redis
Usage
Add muffin_redis to PLUGINS in your Muffin Application configuration.
Or install it manually like this:
redis = muffin_redis.Plugin(**{'options': 'here'}) app = muffin.Application('test') app.install(redis)
Appllication configuration options
REDIS_DB – Number of Redis database (0) REDIS_HOST – Connection IP address (“127.0.0.1”) REDIS_PORT – Connection port (6379) REDIS_PASSWORD – Connection password (None) REDIS_POOLSIZE – Connection pool size (1) REDIS_FAKE – Use fake redis instead real one for tests proposals (False)
Queries
@app.register def view(request): value = yield from app.ps.redis.get('my_key') return value
Pub/Sub
You need to have enabled REDIS_PUBSUB = True in your configuration for this functionality.
Publishing messages is as simple as this:
@app.register def view(request): yield from app.ps.redis.publish('channel', 'message')
Receiving is more complex. In order to start receiving messages from pubsub, you should create a subscription manager. Then you open a separate connection within that manager, after which you can subscribe and listen for messages:
sub = app.ps.redis.start_subscribe() yield from sub.open() # this creates separate connection to redis # sub.open() returns that manager itself, so this can be written like this: # sub = yield from app.ps.redis.start_subscribe().open() sub.subscribe(['channel1']) sub.psubscribe(['channel.a.*', 'channel.b.*']) # you can use masks as well # now wait for new messages while True: msg = yield from sub.next_published() print('got message', msg) print('the message itself:', msg.value) if shall_stop: break yield from sub.close() # don't forget to close connection!
Subscription manager also implements PEP 0492 async with and async for interfaces, so in Python 3.5+ that can be spelled in lighter way:
async with app.ps.redis.start_subscribe() as sub: await sub.subscribe(['channel1']) await sub.psubscribe(['channel.a.*', 'channel.b.*']) # you can use masks as well async for msg in sub: print('got message', msg) # no need to close connection explicitly # as it will be done automatically by context manager.
It might be not very good to create separate Redis connection per each subscription manager (e.g. per each websocket), so that could be improved by managing subscribed channel masks and reusing the same single “pubsub-specific” redis connection.
Bug tracker
If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/muffin-redis/issues
Contributing
Development of Muffin-Redis happens at: https://github.com/klen/muffin-redis
Contributors
klen (Kirill Klenov)
License
Licensed under a MIT license.
If you wish to express your appreciation for the project, you are welcome to send a postcard to:
Kirill Klenov pos. Severny 8-3 MO, Istra, 143500 Russia
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
Built Distribution
File details
Details for the file muffin-redis-1.3.2.tar.gz
.
File metadata
- Download URL: muffin-redis-1.3.2.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.27.0 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab5ef275285b6016c532d7de3843c46b72f3420e913d7d1ca07d2a2659f089d3 |
|
MD5 | e7c668f24290651f2f48a136ef1734cd |
|
BLAKE2b-256 | b6d728993c9b03d86d6b2dff8fd0f69a62649a3f7c5087be0b4a2b5c03ebdb5e |
File details
Details for the file muffin_redis-1.3.2-py2.py3-none-any.whl
.
File metadata
- Download URL: muffin_redis-1.3.2-py2.py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.27.0 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7b3cbecf241383f18f2cff88df15238bce87fc60889fd3f47056a904321c81b |
|
MD5 | 208dee91d8b78dd3d76daf2988b7d2c8 |
|
BLAKE2b-256 | a9f41a04072585864d7af27935846dfd6eed7ae684fbc5a714a6e2aa47997558 |