This file contains hidden or 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 redis | |
# The operation to perform for each event | |
def add_new_win(conn, winner): | |
conn.zincrby('wins_counter', 1, winner) | |
conn.incr('total_games_played') | |
def main(): | |
# Connect to Redis |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) | |
# This time we do a transaction |
This file contains hidden or 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 hidden or 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") |