Skip to content

How to use PyAV with multiprocessing correctly? #1788

@darkAlert

Description

@darkAlert

The following code works correctly on the main thread:

frames_ = np.ones((60, 720, 1280, 3), dtype=np.uint8)
container = av.open(dst_path, mode="w")
stream = container.add_stream("mpeg4", rate=fps)
stream.width = 1280
stream.height = 720
stream.pix_fmt = "yuv420p"
stream.thread_count = 0
stream.thread_type = 'AUTO'

for frame_i in frames_:
    frame = av.VideoFrame.from_ndarray(frame_i, format="rgb24")
    for packet in stream.encode(frame):
        container.mux(packet)

for packet in stream.encode(None):
    container.mux(packet)

container.close()

But if I execute the code in the child process (multiprocessing.Process), the main process hangs on exit (the child process itself terminates normally):

Exception ignored in atexit callback: <function _exit_function at 0x798dedbbec20>
Traceback (most recent call last):
  File "/usr/lib/python3.10/multiprocessing/util.py", line 360, in _exit_function
    _run_finalizers()
  File "/usr/lib/python3.10/multiprocessing/util.py", line 300, in _run_finalizers
    finalizer()
  File "/usr/lib/python3.10/multiprocessing/util.py", line 224, in __call__
    res = self._callback(*self._args, **self._kwargs)
  File "/usr/lib/python3.10/multiprocessing/queues.py", line 199, in _finalize_join
    thread.join()
  File "/usr/lib/python3.10/threading.py", line 1096, in join
    self._wait_for_tstate_lock()
  File "/usr/lib/python3.10/threading.py", line 1116, in _wait_for_tstate_lock
    if lock.acquire(block, timeout):
KeyboardInterrupt: 

I already tried:

  • PYAV_LOGGING=off
  • av.logging.set_level(None)
  • av.logging.restore_default_callback()
  • different thread_type

but it has no effect.

My environment:

  • ubuntu 22.04
  • Python 3.10.12
  • PyAV 9.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions