ビデオストリーミングを実行するには、gstreamerパイプラインを実行する必要があります。GStreamerパイプラインには、が呼び出されるまで終了しないメソッドGObject.MainLoop
を持つオブジェクトが必要です。このために、メインスレッドでGObject.MainLoopインスタンスを実行するメインアプリケーションプロセス()からプロセス()を作成します。問題は、ループがプロセスP2内で無期限に進行し、メインアプリケーションプロセス(P1)からループを終了/終了できないことです。run()
quit()
P2
P1
以下は、シナリオの理解に役立つ可能性のあるコードのセクションです。
'''
start() spawns a new process P2 that runs Mainloop within its main thread.
stop() is called from P1, but does not quit the Mainloop. This is probably because
processes do not have shared memory
'''
from multiprocessing import Process
import gi
from gi.repository import GObject
class Main:
def __init__(self):
self.process = None
self.loop = GObject.MainLoop()
def worker(self):
self.loop.run()
def start(self):
self.process=Process(target=self.worker, args=())
self.process.start()
def stop(self):
self.loop.quit()
次に、プロセス間で「ループ」変数を共有するためにマルチプロセッシングキューを使用しようとしましたが、それでもメインループを終了できません。
'''
start() spawns a new process and puts the loop object in a multiprocessing Queue
stop() calls get() from the loop and calls the quit() method, though it still does not quit the mainloop.
'''
from multiprocessing import Process, Queue
import gi
from gi.repository import GObject
class Main:
def __init__(self):
self.p=None
self.loop = GObject.MainLoop()
self.queue = Queue()
def worker(self):
self.queue.put(self.loop)
self.loop.run()
def start(self):
self.p=Process(target=self.worker, args=())
self.p.start()
def stop(self):
# receive loop instance shared by Child Process
loop=self.queue.get()
loop.quit()
子プロセスP2内でのみアクセス可能なMainLoopオブジェクトのquitメソッドを呼び出すにはどうすればよいですか?
multiprocessing.Process
クラスのモジュールを拡張し、Main
そのrun()
メソッドをオーバーライドして、GObject.Mainloop
インスタンスT1
をそのではなく別のスレッド()内で実際に実行しましたmain thread
。そしてようになります待機通知メカニズム実装main thread
プロセス(のがP2
)の下で行くためにwait-notify
ループと使用multiprocessing.Queue
のメインスレッドにメッセージを転送するP2
とP2
同時に通知されますが。たとえば、オーバーライドされたメソッドでハンドラーが定義されてstop()
いるquit
メッセージを送信するメソッド。このモジュールを拡張して、ハンドラーも定義する必要がある場合は、任意の数のメッセージを解析できます。P2
run()
Child Process
以下は、私が使用したコードスニペットです。
from multiprocessing import Process, Condition, Queue
from threading import Thread
import gi
from gi.repository import GObject
loop=GObject.MainLoop()
def worker():
loop.run()
class Main(Process):
def __init__(self, target=None, args=()):
self.target=target
self.args=tuple(args)
print self.args
self.message_queue = Queue()
self.cond = Condition()
self.thread = None
self.loop = GObject.MainLoop()
Process.__init__(self)
def run(self):
if self.target:
self.thread = Thread(target=self.target, args=())
print "running target method"
self.thread.start()
while True:
with self.cond:
self.cond.wait()
msg = self.message_queue.get()
if msg == 'quit':
print loop.is_running()
loop.quit()
print loop.is_running()
break
else:
print 'message received', msg
def send_message(self, msg):
self.message_queue.put(msg)
with self.cond:
self.cond.notify_all()
def stop(self):
self.send_message("quit")
self.join()
def func1(self):
self.send_message("msg 1") # handler is defined in the overridden run method
# few others functions which will send unique messages to the process, and their handlers
# are defined in the overridden run method above
この方法は私のシーンリオではうまく機能していますが、同じことを行うためのより良い方法があれば提案を歓迎します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加