Created
March 14, 2022 02:54
-
-
Save cassioeskelsen/3911ce7181e9f9a952b6e41ae607278d to your computer and use it in GitHub Desktop.
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 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