2
import asyncio
@asyncio.coroutine
def simple_coroutine():
    yield from asyncio.sleep(1)
    print(threading.get_ident())

print(threading.get_ident())
asyncio.get_event_loop().run_until_complete(simple_coroutine())

The above Python code prints out the same thread id, so I guess coroutines are scheduled in an event loop, which is maintained by a single thread. Am I right? and if I am right, does my guess applies to the concept of coroutine in other languages than Python?

leo
  • 117
  • 5
  • Yes for `asyncio` coroutines, the "event loop runs in a thread and executes all callbacks and tasks in the same thread. While a task is running in the event loop, no other task is running in the same thread." [from docs](https://docs.python.org/3/library/asyncio-dev.html#asyncio-multithreading). See this [answer](https://stackoverflow.com/a/23436125/8944057) for a comparison between threads and coroutines. – eugenhu Feb 03 '18 at 15:16

0 Answers0