Skip to content

Instantly share code, notes, and snippets.

View kristoff-it's full-sized avatar
in your codebase, fixing your bugz

Loris Cro kristoff-it

in your codebase, fixing your bugz
View GitHub Profile
import asyncio
import aioredis
async def add_new_win(pool, winner):
await pool.zincrby('wins_counter', 1, winner)
await pool.incr('total_games_played')
async def main():
# Connect to Redis
async def add_new_win(pool, winner):
await pool.zincrby('wins_counter', 1, winner)
await pool.incr('total_games_played')
async def add_new_win(pool, winner):
task1 = pool.zincrby('wins_counter', 1, winner)
task2 = pool.incr('total_games_played')
await asyncio.gather(task1, task2)
last_id = '$'
while True:
events = await pool.xread(['wins_stream'], latest_ids=[last_id], timeout=0, count=10)
for _, e_id, e in events:
winner = e['winner']
await add_new_win(pool, winner)
last_id = e_id
last_id = '$'
while True:
events = await pool.xread(['wins_stream'], latest_ids=[last_id], timeout=0, count=10)
tasks = []
for _, e_id, e in events:
winner = e['winner']
tasks.append(add_new_win(pool, winner))
last_id = e_id
await asyncio.gather(*tasks)
import asyncio
import aioredis
async def add_new_win(pool, winner):
# Creating tasks doesn't schedule them
# so you can create multiple and then
# schedule them all in one go using `gather`
task1 = pool.zincrby('wins_counter', 1, winner)
task2 = pool.incr('total_games_played')
import asyncio, aioredis
async def main():
pool = await aioredis.create_redis_pool('localhost',
db=0, password=None, ssl=False, minsize=4, maxsize=10, encoding='utf8')
# No need to pluck a single connection
# aioredis will schedule your command to
# a random connection inside the pool.
await pool.set("key", "hello world")
import asyncio, aioredis
async def main():
pool = await aioredis.create_redis_pool('localhost',
db=0, password=None, ssl=False, minsize=4, maxsize=10, encoding='utf8')
# Assuming we're not the only ones using the pool
start_other_coroutines_that_use_redis(pool)
# We reserve a connection for a blocking operation
# script.py
import numpy as np
def hash2list(redis_key):
h = redis_key['value'] # redis_key contains 'key' and 'value'
return [float(h['x']), float(h['y']), float(h['z'])]
def do_mean(acc, x):
if acc is None:
return x
$ redis-cli hset vec:1 x 10 y 5 z 23
$ redis-cli hset vec:2 x 2 y 5 z 5
$ cat script.py | redis-cli -x RG.PYEXECUTE
1) 1) "14.0"
2) "5.0"
3) "6.0"
2) (empty list or set)