Skip to content

Instantly share code, notes, and snippets.

@barrachri
Last active October 27, 2017 12:35
Show Gist options
  • Save barrachri/dddfe447d9f273e313a44c75a94338ad to your computer and use it in GitHub Desktop.
Save barrachri/dddfe447d9f273e313a44c75a94338ad to your computer and use it in GitHub Desktop.
import asyncio
import random
class Genropy:
_number = None
@property
def number(self):
cls = self.__class__
return cls._number
@classmethod
async def gen(cls):
while True:
if cls._number is None:
cls._number = 0
await asyncio.sleep(random.randint(0,5))
yield cls._number
cls._number += 1
service = Genropy()
async def main(id):
async for number in service.gen():
print(f"Main {id} - {number}")
loop = asyncio.get_event_loop()
loop.create_task(main(1))
loop.create_task(main(2))
try:
loop.run_forever()
except KeyboardInterrupt:
loop.close()
print(service.number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment