-1

The library of asyncio in Python, and generally, when we talk about asynchronous programming, I always think about doing “concurrent” I/O operations only on the level thread for optimized CPU use. The library of asyncio has function of asyncio.sleep(seconds), but what disturb me was that sleep operation isn’t I/O operation, sleep operation is done on the kernel level with the CPU hardware without any external devices that can be counted as I/O [my definition for I/O is every hardware except from CPU and RAM]. So why does the asyncio lib (Asynchronous I/O) call this operation as an asynchronous I/O operation? This is not a network interface controller we send requests to or the hard disk. I don’t have a problem with “concurrent” every operation we can on the level thread. However, the name of I/O in the end of the library makes me feel that it isn’t the proper terminology. I will be happy for clarification.

One more related question, does the terminology of asynchronous programming refer to “concurrent” I/O operations only or every operation, including CPU operations like x = x + 1 on the level thread? (I guess the last operation can be done “concurrently” on the level thread, but this will be unnecessary)

Link: https://docs.python.org/3/library/asyncio.html

Code snippet:

import asyncio

async def main():
    print('Hello ...')
    await asyncio.sleep(1)
    print('... World!')

asyncio.run(main())
Yakir
  • 1
  • 2
  • Asyncio.sleep() is a utility function that is very useful in a variety of asyncio situations. Of course it doesn't do any I/O itself. – Paul Cornelius Dec 20 '22 at 02:47

1 Answers1

0

Paraphrasing Wikipedia, "Asynchronous programming" generally refers to the occurrence of events outside of the main program flow and ways of handling such events. As such, asynchronous operations are not necessarily I/O ones.

These asynchronous events are generally handled at the hardware or OS level and it is important to understand that at this level almost anything is asynchronous: jobs are put into queues and scheduled by the OS, then they are regularly polled for completion by the OS which then notifies the main application that the job is done.

Such asynchronous events comprises:

  • Network requests (multiplexed and polled by the OS),
  • Timers (managed by hardware timers and interrupts),
  • Communication with various external devices such as keyboards (hardware interrupts),
  • Communication with internal devices such as the GPU (jobs are committed to command queues),
  • etc.

The purpose of the AsyncIO library is to allow the expression of asynchronous programs in a more "structured" and linear way. As such, it wraps many common asynchronous operations such as I/Os and timers into async-await equivalents. AsyncIO is thus not restricted to only asynchronous I/O operations and one can implement an AsyncIO async-await interface to support GPU for example.

Louis Lac
  • 5,298
  • 1
  • 21
  • 36