I am learning python-asyncio, and I'm trying to write a simple model.
I have a function handling tasks. While handling, it goes to another remote service for data and then prints a message.
My code:
dd = 0 @asyncio.coroutine def slow_operation(): global dd dd += 1 print('Future is started!', dd) yield from asyncio.sleep(10 - dd) # request to another server print('Future is done!', dd) def add(): while True: time.sleep(1) asyncio.ensure_future(slow_operation(), loop=loop) loop = asyncio.get_event_loop() future = asyncio.Future() asyncio.ensure_future(slow_operation(), loop=loop) th.Thread(target=add).start() loop.run_forever() But this code doesn't switch the context while sleeping in:
yield from asyncio.sleep(10 - dd) How can I fix that?