This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async def add_new_win(pool, winner): | |
await pool.zincrby('wins_counter', 1, winner) | |
await pool.incr('total_games_played') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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) |