A little experiment to demonstrate Python's use of threads is indeed concurrency and NOT parallelism
The script is designed such that a bunch of random numbers are processed - this is an attempt at avoiding chances of the execution runtime trying to optimize out some values.
The final value is also printed - not because it's interesting, but so that there is no chance the runtime optimizes for the fact that a variable remains unused.
When running the experiment and displaying htop
in another screen, it is visible that multi-processing is happening in procs
mode (all CPUs blaze a short while),
whilst when I use threads
mode, the processing offers just some blips here and there as the main python process gets scheduled wherever it can.
I also did an implementation in Go which uses goroutines, and observing the behaviour in htop indicates that these are doing some level of leveraging either multiprocessing or OS threads somehow, as go run gorun.go 10
clearly has all processes firing on all cylinder