I am trying to implement the simple idea of passing data from stdin to a coroutine:
import asyncio import sys event = asyncio.Event() def handle_stdin(): data = sys.stdin.readline() event.data = data
This code works fine, however a simplified version of it with a variable instead of an Event object also works:
data = None def handle_stdin(): global data data = sys.stdin.readline() @asyncio.coroutine def tick(): while 1: print('Tick') yield from asyncio.sleep(1) global data if data is not None: print('Data received: {}'.format(data)) data = None
My questions are: is the approach with Event correct? Or is there a better way with other asyncio objects to solve this problem? Then, if the approach with Event is good, is using a variable also good?
Thanks.
Zaur nasibov
source share