我正在尝试创建一个可用作 DBus 服务的 MainObject。此 MainObject 应始终保持对其他对象/进程的响应,并且即使在处理其项目时也应保持这种非阻塞状态。因此,项目在一个单独的线程中一个接一个地处理(队列样式)。您可以通过 DBus 或 CommandLine 将项目添加到 MainObject。我简化了示例(没有 dbus,没有命令行)来显示我的问题。
我的问题是,如果我重新启用“tt.join()”,应用程序按预期工作,但它会阻塞其他进程。难怪, tt.join 使应用程序等到单独的线程完成其工作。另一方面,如果 'tt.join()' 保持禁用状态,应用程序不会阻止外部 dbus 事件,但永远不会出现 'ThreadTest done!' (看实际输出)
我想要的是,我的预期输出,但应用程序应该保持响应。
#!/usr/bin/python2.5
import gobject
import threading
import re
import time
class ThreadTest(threading.Thread):
def __init__(self):
threading.Thread.__init__ (self)
print ' ThreadTest created!'
def run(self):
print ' ThreadTest running ...'
time.sleep(1)
print ' ThreadTest done!'
return True
class MainObject():
def __init__(self):
self.timer = gobject.timeout_add(1000, self.update)
self.loop = gobject.MainLoop()
print 'MainObject created!'
def update(self):
print 'MainObject updating ...'
if self.check_running() == False:
tt = ThreadTest()
tt.start()
#tt.join()
return True
def check_running(self):
running = False
expr = re.compile('ThreadTest')
for threadstr in threading.enumerate():
matches = expr.findall(str(threadstr))
if matches:
running = True
return running
mo = MainObject()
mo.loop.run()
预期输出:
MainObject created!
MainObject updating ...
ThreadTest created!
ThreadTest running ...
ThreadTest done!
MainObject updating ...
ThreadTest created!
ThreadTest running ...
ThreadTest done!
MainObject updating ...
ThreadTest created!
ThreadTest running ...
ThreadTest done!
MainObject updating ...
ThreadTest created!
ThreadTest running ...
ThreadTest done!
MainObject updating ...
ThreadTest created!
ThreadTest running ...
ThreadTest done!
实际输出:
MainObject created!
MainObject updating ...
ThreadTest created!
ThreadTest running ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
MainObject updating ...