Knock on the Python GIL, determine how busy it is.
Project description
GIL Knocker
pip install gilknocker
When you thought the GIL was available, and you find yourself suspecting it might be spending time with another.
You probably want py-spy, however if you're looking for a quick-and-dirty way to slip in a GIL contention metric within a specific chunk of code, this might help you.
How?
Unfortunately, there doesn't appear to be any explicit C-API for checking how busy the GIL is. PyGILState_Check won't really work, that's limited to the current thread. PyInterpreterState is an opaque struct, and the PyRuntimeState and other goodies are private in CPython.
So, in ~200 lines of Rusty code, I've conjured up a basic metric that seems
to align with what is reported by py-spy
when running the same test case.
This works by spawning a thread which, at regular intervals, re-acquires the GIL and checks
how long it took for the GIL to answer.
Note, the polling_interval_micros
and sampling_interval_micros
are configurable. The lower the value, the
more accurate the metric, but will be more likely to slow your
program down.. because it will play a larger role in competing for the GIL's attention.
Use
Look at the tests
from gilknocker import KnockKnock
knocker = KnockKnock(polling_interval_micros=1_000, sampling_interval_micros=10_000, timeout_secs=1)
knocker.start()
... smart code here ...
knocker.contention_metric # float between 0-1 indicating roughly how busy the GIL was.
knocker.reset_contention_metric() # reset timers and meteric calculation
... some more smart code ...
knocker.stop()
knocker.stop() # Idempodent stopping behavior
knocker.contention_metric # will stay the same after `stop()` is called.
knocker.is_running # If you're ever in doubt
How will this impact my program?
Short answer, it depends, but probably not much. As stated above, the more frequent the polling and sampling interval, the more likely non-GIL bound programs will be affected, since there is more room for contention. In GIL heavy programs, the monitoring thread will spend most of its time simply waiting for a lock. This is demonstrated in the benchmarks testing.
In general, it appears that polling_interval_micros=1_000
is a good tradeoff in terms of accurate
GIL contention metric and the resulting sampling_interval_micros=10_000
(defaults to 10x polling interval)
is high enough to relax performance impact a bit.
Below is a summary of benchmarking two different
functions, one which uses the GIL, and one which releases it. For interval=None
this means
no polling was used, effectively just running the function without gilknocker
. Otherwise,
the interval represents the value passed to KnockKnock(polling_interval_micros=interval)
python -m pytest -v --benchmark-only benchmarks/ --benchmark-histogram
------------------------------------------------------------------------------------ benchmark: 18 tests -------------------------------------------------------------------------------------
Name (time in s) Min Max Mean StdDev Median IQR Outliers OPS Rounds Iterations
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_bench[a_little_gil-100000] 1.5368 (2.07) 1.6596 (2.23) 1.5968 (2.14) 0.0476 (130.12) 1.5943 (2.14) 0.0719 (140.14) 2;0 0.6262 (0.47) 5 1
test_bench[a_little_gil-10000] 1.5321 (2.06) 1.5989 (2.14) 1.5618 (2.09) 0.0289 (78.95) 1.5610 (2.09) 0.0510 (99.52) 2;0 0.6403 (0.48) 5 1
test_bench[a_little_gil-1000] 1.5246 (2.05) 1.5298 (2.05) 1.5271 (2.05) 0.0019 (5.12) 1.5269 (2.05) 0.0021 (4.00) 2;0 0.6549 (0.49) 5 1
test_bench[a_little_gil-100] 1.5505 (2.09) 1.5543 (2.08) 1.5528 (2.08) 0.0014 (3.96) 1.5533 (2.08) 0.0018 (3.60) 2;0 0.6440 (0.48) 5 1
test_bench[a_little_gil-10] 1.5863 (2.13) 1.6074 (2.16) 1.5928 (2.14) 0.0088 (23.94) 1.5896 (2.13) 0.0111 (21.60) 1;0 0.6278 (0.47) 5 1
test_bench[a_little_gil-None] 1.5043 (2.02) 1.5067 (2.02) 1.5051 (2.02) 0.0011 (2.95) 1.5044 (2.02) 0.0016 (3.17) 1;0 0.6644 (0.50) 5 1
test_bench[a_lotta_gil-100000] 0.7450 (1.00) 0.7458 (1.0) 0.7455 (1.0) 0.0004 (1.0) 0.7457 (1.0) 0.0005 (1.0) 1;0 1.3413 (1.0) 5 1
test_bench[a_lotta_gil-10000] 0.7471 (1.00) 0.8104 (1.09) 0.7601 (1.02) 0.0281 (76.94) 0.7472 (1.00) 0.0168 (32.82) 1;1 1.3156 (0.98) 5 1
test_bench[a_lotta_gil-1000] 0.7436 (1.0) 0.7472 (1.00) 0.7463 (1.00) 0.0015 (4.11) 0.7470 (1.00) 0.0013 (2.54) 1;1 1.3400 (1.00) 5 1
test_bench[a_lotta_gil-100] 0.7558 (1.02) 0.7680 (1.03) 0.7640 (1.02) 0.0050 (13.56) 0.7644 (1.03) 0.0061 (11.97) 1;0 1.3089 (0.98) 5 1
test_bench[a_lotta_gil-10] 0.7542 (1.01) 0.7734 (1.04) 0.7649 (1.03) 0.0084 (23.05) 0.7669 (1.03) 0.0151 (29.45) 2;0 1.3074 (0.97) 5 1
test_bench[a_lotta_gil-None] 0.7437 (1.00) 0.8490 (1.14) 0.8006 (1.07) 0.0501 (137.15) 0.8074 (1.08) 0.0969 (189.03) 1;0 1.2490 (0.93) 5 1
test_bench[some_gil-100000] 1.4114 (1.90) 1.4131 (1.89) 1.4122 (1.89) 0.0007 (1.81) 1.4121 (1.89) 0.0010 (2.00) 2;0 0.7081 (0.53) 5 1
test_bench[some_gil-10000] 1.4115 (1.90) 1.4258 (1.91) 1.4167 (1.90) 0.0059 (16.03) 1.4141 (1.90) 0.0083 (16.19) 1;0 0.7058 (0.53) 5 1
test_bench[some_gil-1000] 1.4169 (1.91) 1.5793 (2.12) 1.4618 (1.96) 0.0690 (188.82) 1.4232 (1.91) 0.0769 (150.04) 1;0 0.6841 (0.51) 5 1
test_bench[some_gil-100] 1.4468 (1.95) 1.6261 (2.18) 1.5701 (2.11) 0.0752 (205.83) 1.5998 (2.15) 0.1004 (195.70) 1;0 0.6369 (0.47) 5 1
test_bench[some_gil-10] 1.5269 (2.05) 1.9894 (2.67) 1.7037 (2.29) 0.1895 (518.49) 1.7301 (2.32) 0.2692 (524.96) 1;0 0.5870 (0.44) 5 1
test_bench[some_gil-None] 1.4115 (1.90) 1.4267 (1.91) 1.4155 (1.90) 0.0063 (17.33) 1.4136 (1.90) 0.0053 (10.24) 1;1 0.7065 (0.53) 5 1
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for gilknocker-0.3.0-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47b970e20936c678fa88f6266532fdd4859a0d6a541a93adfa9657ff50bdebcd |
|
MD5 | 20017c8d0b623a2a6529734114dde0a2 |
|
BLAKE2b-256 | 9ee59dc9ceed46dd5602f936dac1bf29bef49381a90623e89c3734d70af92dcc |
Hashes for gilknocker-0.3.0-cp311-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38bf206c5b4c601607f227726287f9e55c06787aee5d129efabc3aceabbb71ac |
|
MD5 | c2622766bde714e989168d7192f8f5fd |
|
BLAKE2b-256 | 346a7d0e05fa92026c2ab471e9d42acf7e293abf26a71fd42b3b00156ca5f2c1 |
Hashes for gilknocker-0.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1093a6c072026d5c66c844c48343474f45771fb00b25d96a6fcbc100de69a64 |
|
MD5 | b0c276b055b3c43b18351b3d1548ffc8 |
|
BLAKE2b-256 | 932045fd888308f9f1aee134ee65f67f516720e3ef446f4b7cb14751e23aaf8b |
Hashes for gilknocker-0.3.0-cp311-cp311-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b11150bb30bd1c417adf670ded3a767464fa313e4ac6359d1519867cbfe07947 |
|
MD5 | 82b129c4c6f03716cea8c7132bce74ea |
|
BLAKE2b-256 | 15fed7e9e73817bfe03103c0d1957366ef9bf81ed71ed5c84d02a65878773164 |
Hashes for gilknocker-0.3.0-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f7b67b864e92f07211b4724273093c96bff026fe282d996f24618004d7ad40f |
|
MD5 | 51d68c444d0ccfcb529aae73c2f0b8bf |
|
BLAKE2b-256 | 3c1350db2b8166345e3f417a9ed690a8713a74be367a2286fe50b072722daf89 |
Hashes for gilknocker-0.3.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5882e132b3b6ec050612580f098fdc84ba3911fad882dcd4b868f743c9ea4252 |
|
MD5 | 286018a445754a63be01540e66ba7d7b |
|
BLAKE2b-256 | 80062353c64ef873d97006ae7f47e7039adebc0d5d8f66f0b6ad6eb7ea27c63f |
Hashes for gilknocker-0.3.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5c315bf5f8fde9031018026e6e435e1fb7c6a738d539f5e56633662c5abaecc |
|
MD5 | 722a9134474b66041570011a135037f5 |
|
BLAKE2b-256 | ade5601c474264d3a675a80a830bea3cafc626b8f2f92b2479b261aed18dd8f6 |
Hashes for gilknocker-0.3.0-cp310-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24c27cee33e925eee64c5db695cc285026280f9771a1f20d888364302345bb4e |
|
MD5 | 13e4a15438747731af05d1bdd3ea5469 |
|
BLAKE2b-256 | 4ddc890d303ab2c9c77c05b8a7ac853294bdf6332a90e53d9e8622dfdaa306c9 |
Hashes for gilknocker-0.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 413d5f3a5c42b3e4d88ef6a8653b61ce4d15136cd8cc7a41ed14ae8949134b88 |
|
MD5 | 655a29b5dc0061c0766f2ab69dc9a2fa |
|
BLAKE2b-256 | 0800235a02999c6fb9345baca66acd450e830caaede3e171f513bbb8a54ba6c2 |
Hashes for gilknocker-0.3.0-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | adb888e82b30fd597e820a5c661eba0a5665a72526820c973f454f8ecc892ebe |
|
MD5 | c73b990046b89c16abefbaf03836661a |
|
BLAKE2b-256 | 55e68f56c7ed7d8ba37384376f5faeff10c21885074a4cee8051f53ae9f700b3 |
Hashes for gilknocker-0.3.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bad7c2c3d15654b67477008b84fb83f2df7b5d8c494b2dce95a1d88a7d93d065 |
|
MD5 | d40ad3c87627023dc922dcf942730397 |
|
BLAKE2b-256 | c5e8cec3c494aa0e747e36d7318644ffe659a5d2fd535fdec98a8ebedc3adf38 |
Hashes for gilknocker-0.3.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02f412e673fb7f7cb75d3e4e1159929326578abc5f54cf19181b279b6d712008 |
|
MD5 | 51e61387de36eb14d2f81ea10996b51d |
|
BLAKE2b-256 | 5acede93ca1d5ea1185b33d48caaaabb0f6cef20bbdb9fb1981e86d18fa5e892 |
Hashes for gilknocker-0.3.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3156c59147bbe7f270759ecac093e07bc0814a00f0f1eadc62a375ad4369583d |
|
MD5 | 65be19854e49d32dc42e782391015aa3 |
|
BLAKE2b-256 | 6fdb137fafc777e9e883ecc510c04aa950e8da81fe76148ca0b12d801225fae5 |
Hashes for gilknocker-0.3.0-cp39-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 285d9e6663e40dc0c1deb0ab6a57aff2a923cc3bac2e4a52eaf752ba932abc93 |
|
MD5 | 35bb350eb2cbc590e614953297e3d86f |
|
BLAKE2b-256 | 9d94372aa6b21a2ba2529ac7778924cbe255cd7d3658db8de6aeb57fcd921771 |
Hashes for gilknocker-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0a64150af63b1d3e4445258525b91598449451b89fab8b9667b062fb6f3ce03 |
|
MD5 | f3e7e0fa36e3416c7dbf756cc1d702bb |
|
BLAKE2b-256 | 3ba39c0765a8bb9a4ed8933c455e388df99fed5c00f3a3c9a2f7dfdb76e141c0 |
Hashes for gilknocker-0.3.0-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0343bb24718b3e750a4871184a36e50053ae7bafeb1aff0846e08f22f14f0fc4 |
|
MD5 | da2fa62745859d2faa76d60af7972f95 |
|
BLAKE2b-256 | 44bd3f5fc8008fc527fcfc7740af8590827579571f699d48a7f2005460bebaf4 |
Hashes for gilknocker-0.3.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 047e14665281c1ad2092fc0d6c4dfb76e6eb17eb89c2722267c1ccc4ca3aeadf |
|
MD5 | 12899279c9a9d828e7d918fc57a57036 |
|
BLAKE2b-256 | 9f8d17480dd852fcb8444e6bf369ade6feb2323a33ae8e35af4581f55d74c927 |
Hashes for gilknocker-0.3.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88ce06beec76dcebcdfe34985f44cd01409aa265824b52bc977db832527f6ba7 |
|
MD5 | e98a9e7d2d29d6be88f5a12f72e8551a |
|
BLAKE2b-256 | 24755fda62112b90c527f6a4ef96e84529514414e5c192c833cdaac7d64c55e1 |
Hashes for gilknocker-0.3.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c45ce5712a91c31cd69c11b2821ee98d958d6c1fc27bb8e053d2c61ff7c48d93 |
|
MD5 | dc2179b952ad61fc2e776e0f43d27011 |
|
BLAKE2b-256 | a8860409dfdd9dac9a144978648c9cc6821700ab2acfec35674ed21b568bf17c |
Hashes for gilknocker-0.3.0-cp38-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 341b8c40bd30fac66eae243b4bc200369e7941a3b2b50bace3babd640fc31d24 |
|
MD5 | 77b4e2c9b762ea530d74aa3b1d0f6b38 |
|
BLAKE2b-256 | 45306eb4bddca9a5e56e469f86c5cbca0bc61003a2a95fcd623360e28bc2b1d8 |
Hashes for gilknocker-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a36e1eb4ece52418ecbd84cc28e83692db003620b2f4fb4510d75092482f49aa |
|
MD5 | dce8ed4a05b3ef12c4f239fe138c48ac |
|
BLAKE2b-256 | a01d0e9ee53bc00c5cf620f353068b6a0ba129e2d5f38ebb87707070abf8d367 |
Hashes for gilknocker-0.3.0-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e56157a564d1bf072ba58f5ca786a70079b1c2de5e75d6b16d4d15974ca94eb |
|
MD5 | d08453303f30a74f0f525e568a837849 |
|
BLAKE2b-256 | bd4cb480c51a7ba10d0702becc145cfa93a35391b743be7e3c45bdc6a818cba1 |
Hashes for gilknocker-0.3.0-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b9d70ae48a68a28baa9926bdb0985f5c6a3d17910c41898f40f5a84fd14f51d |
|
MD5 | 572990a0d1288e9eea6f626e6010a6f2 |
|
BLAKE2b-256 | 263eea930bc311726495bd1afd887ead7c39a17cec69ec9da2dc95abf9bae8a7 |
Hashes for gilknocker-0.3.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d145ff0e478cb4be37b7151cf4bf158d89ef454dbb12e5e98814ad8d5cadc538 |
|
MD5 | be63a8528eb067abe20caf3c15e54afd |
|
BLAKE2b-256 | cc034b439c386f3ece6849b2ff6ac137ba617f2d159d3e6ea36aea72f6287ec3 |
Hashes for gilknocker-0.3.0-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cadf3a394546be5c6f605c5b143ab86b4071607ea20e6cd53afeb8912b02e131 |
|
MD5 | 568db7e31bf0476f3f21e34cda7662db |
|
BLAKE2b-256 | 19391d3d28d26277fa6f7970e14736cc02928b2e4169e4205f14985b1c4476d0 |
Hashes for gilknocker-0.3.0-cp37-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fded23dbaa2527b75780ef732c6105c69a674b6eb809c2e7053cb584fe1cdcc |
|
MD5 | 3bdc93c158c8208f38afd69a880bf719 |
|
BLAKE2b-256 | cf098ed2ed932b270e9d94906a0bbd4a5dfd42c353a38eaa204ff0c40c86b771 |
Hashes for gilknocker-0.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42f1dcf91da41d46c4c6dd2e8b595dd83e672650de6b564a228043434f880d72 |
|
MD5 | 8643b96690f27821eeafe576a0655a01 |
|
BLAKE2b-256 | a41147b3d76f6252a88851a7f24de83ffb13c187d10ab3d9a404a0ff851e1d92 |
Hashes for gilknocker-0.3.0-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd6ab94f4e9311bc971cd7b801fcebd6f539801c2f159c171c1a1381ed14e961 |
|
MD5 | 47862c57dc52c4a710f2870b500c66ec |
|
BLAKE2b-256 | 34aaab15a87380c9418e33b9310044c6f0fca79ec6210139c27d84a83423b2d0 |
Hashes for gilknocker-0.3.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21fec76b5f5e980840ef311c076a5c3ddd7dbb9b9a9b57815bb2faf04e4bac5c |
|
MD5 | a522f0d9d03dc77559a90b861ae20523 |
|
BLAKE2b-256 | 88b6f8c7b2e73cbbf690f5fac29ec8a41e9ccbeaadff448cc921e13d6c80cd61 |