Skip to main content

Knock on the Python GIL, determine how busy it is.

Project description

GIL Knocker

pip install gilknocker

Code Style CI PyPI PyPI - Wheel Downloads

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 interval (interval_micros) is configurable. The lower the value, the more accurate the metric, but will be more likely to cause a deadlock and/or 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(interval_micros=1000, timeout_secs=1)
knocker.start()

... smart code here ...

knocker.stop()
knocker.contention_metric  # float between 0-1 indicating roughly how busy the GIL was.

License

Unlicense or MIT, at your discretion.

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

gilknocker-0.2.0.tar.gz (9.6 kB view hashes)

Uploaded Source

Built Distributions

gilknocker-0.2.0-cp311-none-win_amd64.whl (154.4 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

gilknocker-0.2.0-cp311-none-win32.whl (146.4 kB view hashes)

Uploaded CPython 3.11 Windows x86

gilknocker-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (595.0 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

gilknocker-0.2.0-cp311-cp311-manylinux_2_12_i686.manylinux2010_i686.whl (614.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.12+ i686

gilknocker-0.2.0-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (513.3 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

gilknocker-0.2.0-cp311-cp311-macosx_10_7_x86_64.whl (263.2 kB view hashes)

Uploaded CPython 3.11 macOS 10.7+ x86-64

gilknocker-0.2.0-cp310-none-win_amd64.whl (154.4 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

gilknocker-0.2.0-cp310-none-win32.whl (146.4 kB view hashes)

Uploaded CPython 3.10 Windows x86

gilknocker-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (595.0 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

gilknocker-0.2.0-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl (614.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.12+ i686

gilknocker-0.2.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (513.3 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

gilknocker-0.2.0-cp310-cp310-macosx_10_7_x86_64.whl (263.2 kB view hashes)

Uploaded CPython 3.10 macOS 10.7+ x86-64

gilknocker-0.2.0-cp39-none-win_amd64.whl (154.6 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

gilknocker-0.2.0-cp39-none-win32.whl (146.6 kB view hashes)

Uploaded CPython 3.9 Windows x86

gilknocker-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (595.3 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

gilknocker-0.2.0-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl (614.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

gilknocker-0.2.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (513.7 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

gilknocker-0.2.0-cp39-cp39-macosx_10_7_x86_64.whl (263.4 kB view hashes)

Uploaded CPython 3.9 macOS 10.7+ x86-64

gilknocker-0.2.0-cp38-none-win_amd64.whl (154.6 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

gilknocker-0.2.0-cp38-none-win32.whl (146.7 kB view hashes)

Uploaded CPython 3.8 Windows x86

gilknocker-0.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (595.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

gilknocker-0.2.0-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl (614.5 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

gilknocker-0.2.0-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (514.1 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

gilknocker-0.2.0-cp38-cp38-macosx_10_7_x86_64.whl (263.7 kB view hashes)

Uploaded CPython 3.8 macOS 10.7+ x86-64

gilknocker-0.2.0-cp37-none-win_amd64.whl (154.6 kB view hashes)

Uploaded CPython 3.7 Windows x86-64

gilknocker-0.2.0-cp37-none-win32.whl (146.8 kB view hashes)

Uploaded CPython 3.7 Windows x86

gilknocker-0.2.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (595.5 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

gilknocker-0.2.0-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl (614.8 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

gilknocker-0.2.0-cp37-cp37m-macosx_10_7_x86_64.whl (263.8 kB view hashes)

Uploaded CPython 3.7m macOS 10.7+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page