Closed Andrei-Pozolotin closed 4 years ago
Short answer: Without some experiments and data, I have no idea.
My gut feeling is that changing the curio kernel thread priority is going to have little effect (if any) on how Python actually executes threads due to issues in how the GIL is implemented. For example, there is an inherent delay in acquiring the GIL from any CPU-bound thread that is present independently of priorities.
Of course, if the heavy math threads were to run with the GIL released (i.e., involving C extensions, etc.), it might be a different story.
ok, so far:
based on: http://www.yonch.com/tech/82-linux-thread-priority https://gitlab.com/guystreeter/python-hwloc/blob/master/linuxsched.pyx https://github.com/python-llfuse/python-llfuse/blob/master/Include/pthread.pxd
I can set priority to RT/99: https://github.com/random-python/data_pipe/blob/master/src/test/data_pipe_test/verify_priority.py https://github.com/random-python/data_pipe/blob/master/src/main/data_pipe/native_system.pxd https://github.com/random-python/data_pipe/blob/master/src/main/data_pipe/native_module.pyx#L13
but that requires root
do you know if this can be root
-less?
Messing around with thread priorities and scheduling has historically always required root access if your goal is to increase the priority beyond the default setting. A user can usually decrease their scheduling priority however. So, that might be a different option (decrease the scheduling priority of CPU-intensive threads and run everything else at normal priority).
great idea, will try that.
look: secret santa already committed os.setpriority
and os.getpriority
10 years back:
https://bugs.python.org/issue10784
which is present but not documented: Modules/posixmodule.c
meanwhile, using os.nice
, which is documented
the following experiment:
https://gist.github.com/Andrei-Pozolotin/14187e1c61e0fa56866077c5acfd92ce
where 1 master
is running at normal nice
priority and 9 worker
are running at minimal nice
priority, produces "negative result": all 10 thread are performing at even 10%, despite top/htop
confirm proper nice
setting per thread
do you see an error in the experiment's code? if not, where/how GIL manages to mess up the os scheduler?
porting from threading
to multiprocessing
, next experiment:
https://gist.github.com/Andrei-Pozolotin/3e548c640000982de98f0b701c7e0b21
again top/htop
shows proper nice
level, though unlike threading
,
now each process shows 99.9% CPU% (tested on 32 core box, so that seems possible)
the experiment produces the same "negative result", meaning it shows even 10% performance ratio between processes which can be interpreted as:
nice
does not cut it?any insight is much appreciated
@dabeaz David:
given all your deep GIL hacking experience https://www.youtube.com/watch?v=Obt-vMVdM8s
say, an application is running
curio
in main thread as an orchestration framework only (no heavy i/o, no heavy math), then pushing bytes viaasyncio
, and doing heavy math viathreading
- all in separate threads,do you think
curio
kernel thread priority elevation approaches such aspthread_setschedparam()
for Linux andSetThreadPriority()
for Windows can prevent main thread starvation, i.e. protection against both heavy i/o threads and heavy math threads?