3 ms·
In JS, Python and similarly built systems async is inherently coupled to an inversion of control. When you write (Python): await sock.write("fooooooo") w
by formerly_proven 5y ago
In JS, Python and similarly built systems async is inherently coupled to an inversion of control.
When you write (Python):
await sock.write("fooooooo")
write will, after several layers of abstractions and wrappers, look like this (on a reactor/readiness-oriented system):
def write(self, data):
while data:
try:
written = self._socket.write(data)
except EWOULDBLOCK, EAGAIN:
yield 'writable', self._socket
data = data[written:]
"yield" here means to pass a bit of data up to the event loop (neé coroutine scheduler), and that event loop will watch (using select/epoll) for that socket to become writable and then hand control back.
Because this inversion of control doesn't happen unless you (usually) explicitly wrap a coroutine (e.g. asyncio.run_until_complete) with an event loop, you can't just call a coroutine. It doesn't know when to resume in these designs, and your synchronous function doesn't know how to, either.