Skip to content

Instantly share code, notes, and snippets.

@semyont
Created April 12, 2017 20:53
Show Gist options
  • Save semyont/071ed972cdf70a6e96adfeec2989d15f to your computer and use it in GitHub Desktop.
Save semyont/071ed972cdf70a6e96adfeec2989d15f to your computer and use it in GitHub Desktop.
gevent based concurrency for redis-py
import logging
logging.basicConfig(
format='%(asctime)s,%(msecs)05.1f (%(funcName)s) %(message)s',
datefmt='%H:%M:%S')
log = logging.getLogger()
log.setLevel(logging.INFO)
import threading
import os
import time
import gevent
import redis
import redis.connection
redis.connection.socket = gevent.socket
r = redis.StrictRedis()
p = r.connection_pool
def main():
crongreenlet = gevent.spawn(cron)
log.info("Spawning 500 greenlets connecting to Redis...")
redisgreenlets = [gevent.spawn(ask_redis) for _ in xrange(500)]
starttime = time.time()
# Wait until all greenlets have started and connected.
gevent.sleep(1)
log.info("# active `threading` threads: %s" % threading.active_count())
log.info("# Redis connections created: %s" % p._created_connections)
log.info("# Redis connections in use: %s" % len(p._in_use_connections))
log.info("# Redis connections available: %s" % len(p._available_connections))
log.info("Waiting for Redis connection greenlets to terminate...")
gevent.joinall(redisgreenlets)
d = time.time() - starttime
log.info("All Redis connection greenlets terminated. Duration: %.2f s." % d)
crongreenlet.kill()
def ask_redis():
# BRPOP returns (key, value) tuple or None
assert r.brpop("list", timeout=1) is None
def cron():
while True:
log.info("Hello from cron greenlet.")
gevent.sleep(0.2)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment