OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do I properly terminate a python multiprocessing process that runs a GStreamer pipeline

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

Guest
I am running a GStreamer pipeline in a separate Python multiprocessing process. The process captures frames from a camera to a multiprocessing Queue (full_frame_queue). I want to stop the process properly by first stopping the pipeline, and then waiting for the process to join. I am able to set the pipeline to NULL, and the queue is empty, but still the process does not join. Here is how I start the pipeline process:

Code:
    def start(self):
        Gst.init(None)
        self.capture_process = Process(target=self.capture_frame)
        self.capture_process.start()```

Here is the capture_frame method that gets run in the process:

Code:
def capture_frame(self):
    pipeline = self.__start_gstreamer_pipeline()
    full_sink = pipeline.get_by_name("full_sink")
    if not pipeline.set_state(Gst.State.PLAYING):
        raise RuntimeError("Unable to set the pipeline to the playing state.")
    try:
        while self.running.is_set():
            full_sample = full_sink.emit("pull-sample")
            if full_sample:
                self.process_frame(full_sample)
            else:
                time.sleep(0.1)
    finally:
        Gst.Element.send_event(pipeline, Gst.Event.new_eos())
        print("EoS sent")
        pipeline.set_state(Gst.State.NULL)
        print("Switched to NULL state")```

Here is where I initiate stopping the process by clearing the running event, causing capture_frame to enter the finally block:

Code:
    def stop(self):
        if self.capture_process:
            self.running.clear()
            while not not self.full_frame_queue.empty():
                self.full_frame_queue.get()
            self.capture_process.join(timeout=10)
            print("Capture process joined")

Any ideas where I am going wrong?
<p>I am running a GStreamer pipeline in a separate Python multiprocessing process. The process captures frames from a camera to a multiprocessing Queue (<code>full_frame_queue</code>). I want to stop the process properly by first stopping the pipeline, and then waiting for the process to join. I am able to set the pipeline to NULL, and the queue is empty, but still the process does not join. Here is how I start the pipeline process:</p>
<pre><code> def start(self):
Gst.init(None)
self.capture_process = Process(target=self.capture_frame)
self.capture_process.start()```

Here is the capture_frame method that gets run in the process:
</code></pre>
<pre><code>def capture_frame(self):
pipeline = self.__start_gstreamer_pipeline()
full_sink = pipeline.get_by_name("full_sink")
if not pipeline.set_state(Gst.State.PLAYING):
raise RuntimeError("Unable to set the pipeline to the playing state.")
try:
while self.running.is_set():
full_sample = full_sink.emit("pull-sample")
if full_sample:
self.process_frame(full_sample)
else:
time.sleep(0.1)
finally:
Gst.Element.send_event(pipeline, Gst.Event.new_eos())
print("EoS sent")
pipeline.set_state(Gst.State.NULL)
print("Switched to NULL state")```
</code></pre>
<p>Here is where I initiate stopping the process by clearing the <code>running</code> event, causing <code>capture_frame</code> to enter the <code>finally</code> block:</p>
<pre><code> def stop(self):
if self.capture_process:
self.running.clear()
while not not self.full_frame_queue.empty():
self.full_frame_queue.get()
self.capture_process.join(timeout=10)
print("Capture process joined")
</code></pre>
<p>Any ideas where I am going wrong?</p>
 
Top