我试图在几秒钟后停止gobject.MainLoop()
。
我不知道是否可以为这种循环设置一个超时,这将是完美的,但我没有发现。在
所以,我试图用线程来解决这个问题,但不幸的是,主循环阻塞了其他线程。
这里是我的代码(我正在使用python2.7):
import MediaCenter_dbusConfig
import dbus
import gobject
from dbus.mainloop.glib import DBusGMainLoop
from time import sleep
from threading import Thread
mainloop=0
class Timeout(Thread):
global mainloop
def __init__(self):
Thread.__init__(self)
def run(self):
global mainloop
i = 0
while i < 30:
sleep(1)
i += 1
mainloop.quit()
class Loop(Thread):
global mainloop
def __init__(self):
Thread.__init__(self)
def run(self):
global mainloop
sleep(5)
mainloop.run()
def catchall_detectedDevicePopUp_from_willShowPopup_signals_handler(popup_string):
global mainloop
if(popup_string == "DetectedDevicePopUp.qml") :
print(popup_string)
mainloop.quit()
def detectedDevicePopUp_detector() :
global mainloop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus=MediaCenter_dbusConfig.init() # basicly do a dbus.bus.BusConnection()
bus.add_signal_receiver(catchall_detectedDevicePopUp_from_willShowPopup_signals_handler, dbus_interface = "com.orange.mediacenter.apimanager", signal_name = "willShowPopup")
mainloop = gobject.MainLoop()
thread1 = Timeout()
thread2 = Loop()
thread1.start()
thread2.start()
thread1.join()
thread2.join()
我打电话给detectedDevicePopUp_detector()
。我在等一个名为willShowPopup
的信号。如果我收到信号,我想停止循环并继续我的程序,30秒后,如果我没有收到任何信号,我想要相同的东西(停止循环并继续我的程序),但这里它不工作,我的Timeout
线程被我的Loop
线程阻塞。
澄清:我不能编辑发送的信号(我测试一个应用程序)。在
有什么想法吗?在
按照我的理解,线程并不是真正需要的。下面是一个使用
gobject.timeout_add
添加主循环在没有信号停止时运行的最大时间的示例:如果接收到
^{pr2}$Stop
信号,例如:主循环将退出,代码将从调用
loop.run()
的位置继续。在如果没有接收到信号,主循环将在超时时停止(在本例中为30秒),并从调用
loop.run()
的位置继续。在相关问题 更多 >
编程相关推荐