Skip to content

Release the GIL when opening a codec - #2518

Merged
WyattBlue merged 1 commit into
masterfrom
release-gil-codec-open
Nov 18, 2026
Merged

WyattBlue merged 1 commit into
masterfrom
release-gil-codec-open

Conversation

@WyattBlue

Copy link
Copy Markdown
Member

Fixes #2517.

CodecContext.open() called avcodec_open2() with the GIL held, so a slow hardware encoder open (seconds for QSV) stopped every other Python thread. It now releases the GIL there, and around av_hwdevice_ctx_create() and av_hwframe_ctx_init(), the other slow calls in hardware encoder setup.

With the issue's ticker script on macOS (h264_videotoolbox, 1920x1080), the longest pause of another thread during open():

open longest pause
before 74.8 ms 74.8 ms
after 70.9 ms 1.3 ms
before, with hwaccel 52.1 ms 51.7 ms
after, with hwaccel 51.1 ms 3.4 ms

Hardware encoders can take seconds in avcodec_open2(), and every other
Python thread stopped for that time. Also release it around creating a
hardware device and the encoder's hardware frame pool.
@WyattBlue
WyattBlue merged commit ba113e6 into master Nov 18, 2026
10 checks passed
@WyattBlue
WyattBlue deleted the release-gil-codec-open branch November 18, 2026 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Release the GIL in CodecContext.open() (avcodec_open2): hardware encoders block all Python threads for up to seconds

1 participant