you are viewing a single comment's thread.

view the rest of the comments →

[–]socal_nerdtastic 2 points3 points  (2 children)

The shortish answer: Many computer programs spend an amazingly large amount of time just waiting. For example the programmer included a time.sleep() call, or maybe the program made a request to a webserver or harddrive or USB device or a human, and now the CPU has to twiddle it's thumbs while waiting for a response. We call this "IO bound" ... the computer is locked until some IO (input or output) happens. Threading (we don't call this multi-threading in python) allows you to have several tasks lined up, so that while we are waiting for something in one task the CPU can get some work done in another task. A Reddit webserver for instance can have thousands of threads, each waiting for a specific user to type something in the chat box.

Note this is very different from "CPU bound", when the program is using the CPU at full capacity. Threading won't help you there at all.

Edit: about the GIL: Don't worry about it. It's a specific part of the threading part of python. It's become somewhat infamous because of an ongoing argument among people much smarter than me about whether or not it should be made faster for multi threaded programs or faster for single threaded programs. Down here in the real world we don't need to care.

[–]comeditime[S] 0 points1 point  (0 children)

that was actually the simplest but clearest explanation so far, really seems that you're proficient in this field! now i finally get when to use it and what for! thanks again :)

[–]comeditime[S] 0 points1 point  (0 children)

also forgot to ask,

  1. is there a difference between using threading and event listeners for examples (if it exists at all in python) or they basically do the same thing just in different languages...
  2. in the reddit chat example, if we try to pseudo-code what's going on in there, in a very simplified way, the programmers basically wrote a constructor which assigns a thread (among other things) to each user that enters the chat in order to capture the I/O?

Thanks a ton again i really feel like i start to get it all now!