2

It seems when I google what I\O is, I get hits that show it stands for "Input" and "Output". I see blogs on Python titled, "Python File I\O --Part 19, Advanced File Input and Output " If I go to the Python docs and search for input/output: https://docs.python.org/3/tutorial/inputoutput.html The examples are limited to print statements and reading/writing files.

I see no examples of web-services or networking as Input/Output in the "Input and Output" section of the Python docs, despite seeing the term I/O used often with these concepts. Is I/O containing the same meaning when referring to web-services and networks as well?

I've been doing a lot of reading on the GIL and multi-threading recently and the term I/O bound has popped up in addition to I/O.

I/O bound seems to refer to a state where there is a lot of CPU idleness, due to the slowness of getting data to the CPU. Multi-threading seems to be used often with Webservices and Networking because I'm assuming there is a lot of CPU idleness, i.e I/O bound heavy tasks because you are always for waiting a user-input to act upon.

Now are the print statements as well read/write files also considered I/O bound tasks?

Finally, the term I/O has also popped up in the Python Docs regarding GIL:

"The GIL is controversial because it prevents multithreaded CPython programs from taking full advantage of multiprocessor systems in certain situations. Note that potentially blocking or long-running operations,, such as I/O image processing, and NumPy number crunching, happen outside the GIL. Therefore it is only in multithreaded programs that spend a lot of time inside the GIL, interpreting CPython bytecode, that the GIL becomes a bottleneck."

So the reference to I/O in this text, says that all I/O tasks bypass the GIL -- so does that mean print, reading and write to files, webservices, networking etc -- all these types of tasks bypass the GIL and are can be exploited by multithreading?

Thank you.

Moondra
  • 4,399
  • 9
  • 46
  • 104
  • Operations in the core interpreter or extension modules that may block or otherwise not need the interpreter for a relatively long time will typically release the GIL beforehand to allow another thread to access the interpreter. When the operation completes the thread has to wait to reacquire the GIL before it can resume executing Python bytecode. – Eryk Sun Jul 28 '17 at 00:35
  • Thank you. How exactly is this determined? Is it the interpreter or OS that determines which operations will take a long time and which won't? Is this hard-coded in the interpreter or a behavior of the OS? – Moondra Jul 28 '17 at 02:37
  • The GIL is a lock that's managed within the Python process. Other than the fact that basic primitives (e.g. semaphores) depend on the OS, the high-level behavior is a function of the implementation of CPython. The points that release the GIL are almost always hard-coded. For example, a function will use the `Py_BEGIN_ALLOW_THREADS` macro to release the GIL before making a system call to [read from a file](https://github.com/python/cpython/blob/v3.6.2/Python/fileutils.c#L1191). – Eryk Sun Jul 28 '17 at 02:58

1 Answers1

2

Yes, I/O stands for Input/Output. This is from the point of view of your program. Input can come from many different sources, such as a keyboard, mouse, or network connection. Similarly, Output can be sent to many different sources. Most programming languages define input and output operations which can work with a variety of sources and destinations. This makes is much easier to write code because we just receive and send data without worrying about the details of where it comes from or goes to.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268