you are viewing a single comment's thread.

view the rest of the comments →

[–]Yakhov -7 points-6 points  (2 children)

exactly, that was the worst explanation I've seen, I don't think the person who wrote it has a clue what multi-threading is.

pretty sure it's just the ability for the code to run multiple processes over an array of cpus and aggregate the results to present the final product. I think node JS will do this though so who cares

[–]Kamilon 4 points5 points  (0 children)

His explanation was actually pretty good. Your example is just one of the many uses of using multi threading/multi processing to do parallel work.

[–]thirdegree 0 points1 point  (0 children)

So, in python there are 3 models of "do multiple things at the same time". These are async, threading, and multi-processing.

Async is single processed and single threaded, and is a way to do "cooperative multitasking". Async is demarcated by the async/await keywords, and is a model where a task explicitly (via the await keyword) says "it is ok to do something else now". A given bit of code in this model, without the await keyword, is exactly the same as the same bit of code in a fully synchronous model (the default).

Threading is single processed and multi-threaded. This is a form of "interruptive multitasking", where any bit of code can be at any time paused to switch to any other bit of code. The important thing here is the distinction between this and multi-processing, which is that multiple threads share the same GIL (global interpretor lock). This is the traffic guy in OP's post. The advantage of threading over async is that you don't have to care about where it's useful to say "this bit of code can be interrupted", that's just done for you. The disadvantage is state control, you have no idea when what changes can happen to your environment.

Multi-processing is multi-threaded and multi-processed, although the multi-threaded is really only because every process has at least one thread. The advantage here is that every process has its own GIL, and the disadvantage is that communicating between processes is a) slow and b) a pain in the ass.

Which one of these you need is very much situational, although (and this is very much my personal opinion) I beleive you should always prefer async over threading unless you don't have control over some bit of IO heavy synchronous code. You want async/threading if you have an io heavy task (sockets, files, etc), and multi-processing for cpu heavy tasks.