Skip to content

Instantly share code, notes, and snippets.

@cassioeskelsen
Created March 14, 2022 02:54
Show Gist options
  • Select an option

  • Save cassioeskelsen/3911ce7181e9f9a952b6e41ae607278d to your computer and use it in GitHub Desktop.

Select an option

Save cassioeskelsen/3911ce7181e9f9a952b6e41ae607278d to your computer and use it in GitHub Desktop.
async def main():
global total_votes
async with redis.client() as conn:
while True:
chunk = []
votes = {}
for _ in range(0, 1000):
item = await conn.lpop("votos")
if item is not None:
obj = json.loads(item)
option = "votos_" + obj["body"].split("=")[1]
if option in votes:
votes[option] += 1
else:
votes[option] = 1
chunk.append(obj)
else:
break
if chunk:
for option in votes.keys():
await conn.incrby(option, amount=votes[option])
await conn.rpush("votos_bd", json.dumps(chunk))
total_votes += len(chunk)
else:
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment