redis-py

Fail-safe message broadcasting to be consumed by a specific recipient using redis and python

怎甘沉沦 提交于 2020-02-25 04:00:40
问题 So redis 5.0 freshly introduced a new feature called Streams. They seem to be perfect for distributing messages for inter process communication: they surpass the abilities of PUB/SUB event messaging in terms of reliability: PUB/SUB is fire-and-forget an there's no guarantee a recipient will receive the message redis lists are somewhat low-level but could still be used. However, streams are optimized for performance and exactly the use case described above. However, since this feature is quite

Fail-safe message broadcasting to be consumed by a specific recipient using redis and python

三世轮回 提交于 2020-02-25 04:00:09
问题 So redis 5.0 freshly introduced a new feature called Streams. They seem to be perfect for distributing messages for inter process communication: they surpass the abilities of PUB/SUB event messaging in terms of reliability: PUB/SUB is fire-and-forget an there's no guarantee a recipient will receive the message redis lists are somewhat low-level but could still be used. However, streams are optimized for performance and exactly the use case described above. However, since this feature is quite

Storing postgresql timestamptz as score to redis sorted set via redis-py : DataError

霸气de小男生 提交于 2020-01-25 05:10:05
问题 I am storing postgresql timestamptz to redis sorted set using redis-py. timestamptz is used as score and data is used as value. I need the set to be sorted in descending order.But I can't insert the data into redis.I don't know how to convert into redis-supported format. Here's the code: cursor.execute("select current_timestamp;"); timestamp_raw=cursor.fetchone() redis_client.zadd("stream",{data:timestamp_raw}) Error is below .....\AppData\Local\Programs\Python\Python38-32\lib\site-packages

Redis: # of channels degrading latency. How to prevent degradation?

一曲冷凌霜 提交于 2019-12-11 05:52:38
问题 pub.py import redis import datetime import time import json import sys import threading import gevent from gevent import monkey monkey.patch_all() def main(chan): redis_host = '10.235.13.29' r = redis.client.StrictRedis(host=redis_host, port=6379) while True: def getpkg(): package = {'time': time.time(), 'signature' : 'content' } return package #test 2: complex data now = json.dumps(getpkg()) # send it r.publish(chan, now) print 'Sending {0}'.format(now) print 'data type is %s' % type(now)

Faster way to iterate all keys and values in redis db

半腔热情 提交于 2019-12-11 00:55:38
问题 I have a db with about 350,000 keys. Currently my code just loops through all keys and gets its value from the db. However this takes almost 2 minutes to do, which seems really slow, redis-benchmark gave 100k reqs/3s. I've looked at pipelining but I need each value returned so that I end up with a dict of key, value pairs. At the moment I'm thinking of using threading in my code if possible to speed this up, is this the best way to handle this usecase? Here's the code I have so far. import

Terminate a hung redis pubsub.listen() thread

不羁岁月 提交于 2019-12-10 17:37:59
问题 Related to this question I have the following code which subscribes to a redis pubsub queue and uses the handler provided in __init__ to feed the messages to the class that processes them: from threading import Thread import msgpack class Subscriber(Thread): def __init__(self, redis_connection, channel_name, handler): super(Subscriber, self).__init__(name="Receiver") self.connection = redis_connection self.pubsub = self.connection.pubsub() self.channel_name = channel_name self.handler =

redis.exceptions.ConnectionError: Error -2 connecting to localhost:6379. Name or service not known

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 13:39:46
问题 I have this error when I run my code in server, my env is debian, and Python2.7.3 Traceback (most recent call last): File "fetcher.py", line 4, in <module> import mirad.fetcher_tasks as tasks File "/home/mirad/backend/mirad/fetcher_tasks.py", line 75, in <module> redis_keys = r.keys('*') File "/home/mirad/backend/venv/local/lib/python2.7/site-packages/redis/client.py", line 863, in keys return self.execute_command('KEYS', pattern) File "/home/mirad/backend/venv/local/lib/python2.7/site

How to use sadd with multiple elements in Redis using Python API?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 19:56:16
问题 Please consider the following example >>import redis >>redis_db_url = '127.0.0.1' >>r = redis.StrictRedis(host = redis_db_url,port = 6379,db = 0) >>r.sadd('a',1) >>r.sadd('a',2) >>r.sadd('a',3) >>r.smembers('a') [+] output: set(['1', '3', '2']) >>r.sadd('a',set([3,4])) >>r.smembers('a') [+] output: set(['1', '3', '2', 'set([3, 4])']) >>r.sadd('a',[3,4]) >>r.smember('a') [+] set(['1', '[3, 4]', '3', '2', 'set([3, 4])']) According to the official documentation in https://redis-py.readthedocs

Is non-blocking Redis pubsub possible?

孤街浪徒 提交于 2019-12-03 04:40:59
问题 I want to use redis' pubsub to transmit some messages, but don't want be blocked using listen , like the code below: import redis rc = redis.Redis() ps = rc.pubsub() ps.subscribe(['foo', 'bar']) rc.publish('foo', 'hello world') for item in ps.listen(): if item['type'] == 'message': print item['channel'] print item['data'] The last for section will block. I just want to check if a given channel has data, how can I accomplish this? Is there a check like method? 回答1: I don't think that would be

Redis Python - how to delete all keys according to a specific pattern In python, without python iterating

亡梦爱人 提交于 2019-11-30 04:20:46
I'm writing a django management command to handle some of our redis caching. Basically, I need to choose all keys, that confirm to a certain pattern (for example: "prefix:*") and delete them. I know I can use the cli to do that: redis-cli KEYS "prefix:*" | xargs redis-cli DEL But I need to do this from within the app. So I need to use the python binding (I'm using py-redis). I have tried feeding a list into delete, but it fails: from common.redis_client import get_redis_client cache = get_redis_client() x = cache.keys('prefix:*') x == ['prefix:key1','prefix:key2'] # True # And now cache.delete