Skip to content

Commit fae6e44

Browse files
author
root
committed
Add nb_threads parameter to Graph for configurable thread control
Add a configurable `nb_threads` parameter to `Graph.__cinit__` to allow users to control the number of threads used by the filter graph. Features: - `nb_threads=0` (default): Use FFmpeg's auto-detection (no behavior change) - `nb_threads=1`: Single-threaded execution (safer for some use cases) - `nb_threads=N`: Use N threads for filter graph execution - Environment variable `PYAV_FILTERGRAPH_THREADS` can override the setting This provides flexibility for users who need single-threaded execution for thread safety reasons, while maintaining backward compatibility with the default behavior.
1 parent 8fb7d1d commit fae6e44

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

av/filter/graph.pyx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import warnings
23
from fractions import Fraction
34

@@ -12,8 +13,29 @@ from av.video.frame cimport VideoFrame
1213

1314

1415
cdef class Graph:
15-
def __cinit__(self):
16+
def __cinit__(self, int nb_threads=0):
17+
"""
18+
Create a filter graph.
19+
20+
Args:
21+
nb_threads: Maximum number of threads used by the filter graph.
22+
0 (default) means auto-detect based on available CPU cores.
23+
1 means single-threaded execution (safer for some use cases).
24+
Can also be set via PYAV_FILTERGRAPH_THREADS environment variable.
25+
"""
1626
self.ptr = lib.avfilter_graph_alloc()
27+
28+
# Allow environment variable to override
29+
env_threads = os.environ.get("PYAV_FILTERGRAPH_THREADS")
30+
if env_threads is not None:
31+
try:
32+
nb_threads = int(env_threads)
33+
except ValueError:
34+
pass
35+
36+
if nb_threads > 0:
37+
lib.av_opt_set_int(self.ptr, "threads", nb_threads, 0)
38+
1739
self.configured = False
1840
self._name_counts = {}
1941

0 commit comments

Comments
 (0)