python - Why are threads spread between CPUs? -


i trying head around threading vs. cpu usage. there plenty of discussions threading vs. multiprocessing (a overview being this answer) decided test out launching maximum number of threads on 8 cpu laptop running windows 10, python 3.4.

my assumption threads bound single cpu.

edit: turns out not assumption. understand multithreaded code, one piece of python code can run @ once (no matter where/on core). different multiprocessing code (where processes independent , run indeed independently).
while read these differences, one answer clarified point.

i think explains cpu view below: average view of many threads spread out on many cpus, 1 of them running @ 1 given time (which "averages" of them running time).

it not duplicate of linked question (which addresses opposite problem, i.e. threads on 1 core) , leave hanging in case has similar question 1 day , helped enlightenment.


the code

import threading import time   def calc():     time.sleep(5)     while true:         = 2356^36  n = 0 while true:     try:         n += 1         t = threading.thread(target=calc)         t.start()     except runtimeerror:         print("max threads: {n}".format(n=n))         break     else:         print('.')  time.sleep(100000) 

led 889 threads being started.

enter image description here

the load on cpus distributed (and surprisingly low pure cpu calculation, laptop otherwise idle empty load when not running script):

enter image description here

why so? threads moved pack between cpus , see average (the reality being @ given moment threads on 1 cpu)? or indeed distributed?

as of today still case 'one thread holds gil'. 1 thread running @ time.

the threads managed on operating system level. happens every 100 'ticks' (=interpreter instruction) running thread releases gil , resets tick counter.

because threads in example continuous calculations, tick limit of 100 instructions reached fast, leading immediate release of gil , 'battle' between threads starts acquire gil.

so, assumption operating system has higher expected load , because of (too) fast thread switching + continuous releasing , acquiring gil. os spends more time on switching doing useful calculation.

as mention yourself, using more 1 core @ time, it's better @ multiprocessing modules (joblib/parallel).

interesting read: http://www.dabeaz.com/python/understandinggil.pdf


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -