Last active
October 27, 2017 12:35
-
-
Save barrachri/dddfe447d9f273e313a44c75a94338ad to your computer and use it in GitHub Desktop.
This file contains 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 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