Skip to content

Instantly share code, notes, and snippets.

@egeromin
Created November 3, 2022 11:40
Show Gist options
  • Save egeromin/6348ed27debf510215266e07e7c5309e to your computer and use it in GitHub Desktop.
Save egeromin/6348ed27debf510215266e07e7c5309e to your computer and use it in GitHub Desktop.
import asyncio
from collections import defaultdict
from async_lru import alru_cache
num_email_confirmations = defaultdict(int)
@alru_cache(maxsize=500)
async def send_email_confirmation(user_email: str) -> int:
num_email_confirmations[user_email] += 1
await asyncio.sleep(1)
return num_email_confirmations[user_email]
async def send_confirmations() -> int:
send_once = asyncio.create_task(send_email_confirmation("albert.einstein@example.com"))
send_again = asyncio.create_task(send_email_confirmation("albert.einstein@example.com"))
await send_once
return await send_again
def main():
result = asyncio.run(send_confirmations())
assert result == 1
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment