在wx python应用程序中使用线程和pyGST

时间:2021-11-26 07:12:53

OK, so I am writing an app, which plays music with the pyGST bindings.

好的,所以我正在编写一个应用程序,它使用pyGST绑定播放音乐。

This requires the use of threads to handle playback. The bindings library handles most of the thread control for me, which is nice(and what I was looking for in them).

这需要使用线程来处理回放。绑定库为我处理大部分线程控制,这很好(以及我在其中寻找的内容)。

Now, I don't have a full grasp on this concept, so I would be eager for some references. But the way I understand it, is I have to basically inform the app that it can use multiple threads.

现在,我没有完全掌握这个概念,所以我渴望得到一些参考。但我理解它的方式是,我必须基本上通知应用程序它可以使用多个线程。

I gathered this from the examples on the gstreamer site, where they use this call:

我从gstreamer网站上的例子中收集了这个,他们使用这个电话:

gtk.gdk.threads_init()  
gtk.main()

according to here, this tells the app it can use multiple threads(more or less), which is where my above assumption came from.

根据这里,这告诉应用程序它可以使用多个线程(或多或少),这是我上面的假设来自。

That is the background. Now get this. I have placed those lines in my code, and they work fine. My app plays music rather than crashing whenever it tries. But something doesn't feel right.
In the examples that I got those lines from, they use gtk for the whole GUI, but I want to use wxWidgets, so it feels wrong calling this gtk function to do this.

这就是背景。现在得到这个。我已将这些行放在我的代码中,它们工作正常。我的应用程序播放音乐而不是在尝试时崩溃。但有些事情感觉不对劲。在我从中获取这些行的示例中,他们使用gtk作为整个GUI,但我想使用wxWidgets,所以调用此gtk函数来执行此操作会感觉不对。

Is there a wx equivalent to this? or is it ok to use this, and will it still work cross platform?

有没有相当于wx的wx?或者可以使用它,它还可以跨平台工作吗?

Also, I have to figure out how to kill all these threads on exit(which it does not do right now) I see how they do it in the example using a gtk method again, so again, looking for a wx equivalent.

另外,我必须弄清楚如何在退出时杀死所有这些线程(现在它没有做)我看到他们如何在示例中再次使用gtk方法,所以再次寻找wx等效。

PS: I think this(or the solution) may be related to the wx.App.MainLoop() function, but I am lost in trying to understand how this loop works, so again, good references about this would be appreciated, but I suppose not necessary as long as I have a good solution.

PS:我认为这个(或者解决方案)可能与wx.App.MainLoop()函数有关,但我在试图理解这个循环是如何工作的时候迷失了,所以再一次,关于这个的好参考将会受到赞赏,但我假设没有必要,只要我有一个好的解决方案。

3 个解决方案

#1


Try using this instead:

请尝试使用此代码:

import gobject
gobject.threads_init()

I wonder how come it is not written in large print at the start of every python gstreamer plugin piece of documentation: it only took me several hours to find it.

我想知道在每个python gstreamer插件文档的开头是不是用大字写的:它只花了我几个小时才能找到它。

A bit more details here.

这里有更多细节。

#2


I have no experience with pyGST, but the general advice for using threads and wxPython is to only update the GUI from the main thread (i.e. the thread that starts the MainLoop). See http://wiki.wxpython.org/LongRunningTasks for more information.

我没有使用pyGST的经验,但是使用线程和wxPython的一般建议是仅从主线程(即启动MainLoop的线程)更新GUI。有关更多信息,请参阅http://wiki.wxpython.org/LongRunningTasks。

#3


I have no experience with the python bindings, but I have had success using wxWidgets and GStreamer together on Windows. The problem is that wxWidgets runs a Windows event loop while GStreamer uses a GLib event loop. If you don't care about any of the GStreamer events, you shouldn't need to do anything. However, if you care to receive any of the GStreamer events, you will have to run your own GLib event loop (GMainLoop) in a separate thread with a separate GMainContext. Use gst_bus_create_watch to create a GST event source, add a callback to the source with g_source_set_callback, and then attach it to the main context of your GLib event loop with g_source_attach. You can then handle the GST in the callback, for example, to forward the events to the wx main event loop.

我没有使用python绑定的经验,但我在Windows上使用wxWidgets和GStreamer成功了。问题是wxWidgets运行Windows事件循环,而GStreamer使用GLib事件循环。如果您不关心任何GStreamer事件,您不需要做任何事情。但是,如果您想接收任何GStreamer事件,则必须在具有单独GMainContext的单独线程中运行您自己的GLib事件循环(GMainLoop)。使用gst_bus_create_watch创建GST事件源,使用g_source_set_callback向源添加回调,然后使用g_source_attach将其附加到GLib事件循环的主上下文。然后,您可以在回调中处理GST,例如,将事件转发到wx主事件循环。

#1


Try using this instead:

请尝试使用此代码:

import gobject
gobject.threads_init()

I wonder how come it is not written in large print at the start of every python gstreamer plugin piece of documentation: it only took me several hours to find it.

我想知道在每个python gstreamer插件文档的开头是不是用大字写的:它只花了我几个小时才能找到它。

A bit more details here.

这里有更多细节。

#2


I have no experience with pyGST, but the general advice for using threads and wxPython is to only update the GUI from the main thread (i.e. the thread that starts the MainLoop). See http://wiki.wxpython.org/LongRunningTasks for more information.

我没有使用pyGST的经验,但是使用线程和wxPython的一般建议是仅从主线程(即启动MainLoop的线程)更新GUI。有关更多信息,请参阅http://wiki.wxpython.org/LongRunningTasks。

#3


I have no experience with the python bindings, but I have had success using wxWidgets and GStreamer together on Windows. The problem is that wxWidgets runs a Windows event loop while GStreamer uses a GLib event loop. If you don't care about any of the GStreamer events, you shouldn't need to do anything. However, if you care to receive any of the GStreamer events, you will have to run your own GLib event loop (GMainLoop) in a separate thread with a separate GMainContext. Use gst_bus_create_watch to create a GST event source, add a callback to the source with g_source_set_callback, and then attach it to the main context of your GLib event loop with g_source_attach. You can then handle the GST in the callback, for example, to forward the events to the wx main event loop.

我没有使用python绑定的经验,但我在Windows上使用wxWidgets和GStreamer成功了。问题是wxWidgets运行Windows事件循环,而GStreamer使用GLib事件循环。如果您不关心任何GStreamer事件,您不需要做任何事情。但是,如果您想接收任何GStreamer事件,则必须在具有单独GMainContext的单独线程中运行您自己的GLib事件循环(GMainLoop)。使用gst_bus_create_watch创建GST事件源,使用g_source_set_callback向源添加回调,然后使用g_source_attach将其附加到GLib事件循环的主上下文。然后,您可以在回调中处理GST,例如,将事件转发到wx主事件循环。