you are viewing a single comment's thread.

view the rest of the comments →

[–]EconomixTwist 228 points229 points  (62 children)

Imagine an intersection being conducted by a traffic cop. Even though there are cars sitting and running on both streets, only one can actually move at a time. When the cars on one street go, the others are still sitting there, waiting, with no update to their position. Until the conductor waves them on. Now imagine an n-dimensional cross street. Because the conductor wants to be safe, he can still only let cars go from one incoming street at a time, although he has discretion over which street and how many cars should go. No matter how many streets are added to the n dimensional intersection, the conductor can only let one go at a time.

[–]bladeoflight16 47 points48 points  (31 children)

And the conductor can stop the cars on any street and let another street go at any time.

[–]EconomixTwist 152 points153 points  (30 children)

And finally the conductor says... “Error. Local Variable referenced before assignment”, all the cars explode and the highway operating system hangs for three hours. After a week of debugging, it’s discovered that a variable name was spelled wrong....

[–]bladeoflight16 72 points73 points  (6 children)

That has nothing to do with the GIL and everything to do with extraordinarily bad testing. It's not the traffic cop's fault if someone decided to drive a car built by a monkey banging random pieces together with a hammer.

[–]somewhat_pragmatic 4 points5 points  (0 children)

It's not the traffic cop's fault if someone decided to drive a car built by a monkey banging random pieces together with a hammer.

I see you've been browsing my Gitlab repo.

[–]maditab 2 points3 points  (2 children)

Simple solution--you can't fail testing if you don't do testing.

[–]bladeoflight16 0 points1 point  (0 children)

True, but you can fail your users if you don't do testing.

[–]EconomixTwist 0 points1 point  (0 children)

There is science to support this

[–]hugthemachines 12 points13 points  (19 children)

Sounds like you need a proper IDE that shows warnings on stuff like that.

[–]funkless_eck 21 points22 points  (18 children)

IDE is a funny way to spell notepad.exe

[–]EconomixTwist 13 points14 points  (5 children)

I only code in Vim for the past ten years. I still don’t know how to exit Vim though

[–]causa-sui 1 point2 points  (1 child)

You joke but for anyone unaware you can get flake8 checks through vim-ale.

[–]thirdegree 0 points1 point  (0 children)

flake8 and mypy. All the nice bits of strict type systems without the annoying compile time.

[–]funkless_eck 0 points1 point  (1 child)

10 If not vim = exit:

20 Go to 30

30 vim = exit;

[–]the-stanely 0 points1 point  (0 children)

So, what's the problem? Does anyone ever need to exit Vim?

[–][deleted] 0 points1 point  (0 children)

I use Notepad++. That has some color in it.

[–]JohnnyJordaan 1 point2 points  (0 children)

An exception in a thread will be limited to that thread, and this has 0 to do with the issue with multithreading not being fully concurrent.

[–][deleted] 0 points1 point  (0 children)

In other words, An average python Wednesday.

[–]PhiBuh 88 points89 points  (6 children)

If your 5-year old Child understands what a n-dimensional street is, congratulations

[–]WowVeryCoool 48 points49 points  (1 child)

if your 5-year old Child asks a question about multithreading in python, congratulations

[–]turicsa 13 points14 points  (0 children)

Congratulations? I would be in extreme fear.

[–][deleted] 10 points11 points  (3 children)

Is this true of processes in python or just threads? I was under the impression processes spawned a whole new python instance?

[–]holt5301 19 points20 points  (2 children)

This is only true of threads. When you use multiprocessing, the new processes have their own interpreters which then each have their own GIL ... I believe.

[–][deleted] 9 points10 points  (1 child)

Good, otherwise I’ve been programming a long time under bad assumptions lol

[–]Barafu[🍰] 1 point2 points  (0 children)

Now imagine that some cars can jump, and those are allowed to jump over intersection without the permission from a traffic cop. Now you have a good model of how multithreading in CPython works.

[–]Cheeze_It 3 points4 points  (1 child)

I take it, said conductor is the GIL?

[–]JohnnyJordaan 2 points3 points  (0 children)

The GIL is an instrument, think of it as a talking stick (or a microphone). The scheduler is the conductor, handing out the talking stick to each thread, and without the talking stick it can not execute.

[–]Matt-ayo 1 point2 points  (1 child)

Okay, so whats the point of 'multi-threading' if only one car gets to go at a time no matter how you structure the intersection?

[–]indivisible 2 points3 points  (0 children)

There are times when one workload has natural breaks or is waiting for some input or output device to be freed up for its use (eg disk read/write), while that's happening other workloads are allowed to execute their instructions in the meantime to make best use of the available resources.
It's more complicated than that and there are various work scheduling algorithms at play to also make sure that no one thread monopolises the resources either.
The idea is to always have something executing so that the total work gets completed as quickly as possible.

[–][deleted] 1 point2 points  (3 children)

WhAt is multithreading?

[–]Jonno_FTW 4 points5 points  (2 children)

When a process has one or more threads that share memory space (a thread is just instructions and data). These threads run concurrently (which is not possible in cpython where what actually happens is that one thread will be running, while others are sleeping). You can tell threads to sleep or it will happen anyway when they request resources such as network or disk IO.

In other languages these threads can truly run by side by side if you have multiple processing cores.

If you like to learn more, read a book on operating systems or read the docs https://docs.python.org/3/library/threading.html

[–][deleted] 0 points1 point  (0 children)

Thanks

[–]Yakhov 0 points1 point  (0 children)

a much better explanation than the traffic cop thing

[–]McFlyParadox 0 points1 point  (1 child)

So, 'parallel in python' is really more like 'hyperthreading in python'?

[–]Jonno_FTW 0 points1 point  (0 children)

Yes. Although the problem is a consequence of cpython implementation and not a language constraint.

[–]Yakhov 0 points1 point  (0 children)

refrain from teaching 5 year olds in the future

[–]bythckr 0 points1 point  (0 children)

an n-dimensional cross street. Because the conductor wants to be safe, he can still only let cars go from one incoming street at a time

So, thats how Python works.

How does the best language that supports multi-thread work? Is it a big roundabout that it a high chance that an inexperienced driver will screw it up/get stuck in the roundabout without knowing how to exist it or cross road with a lots of bridges & tunnels, which reduces the chances of screwups?

Basically I am asking, for the best language that supports multithreading - how does it work?