기본적으로 DeviceAdded DBus 이벤트 (예 : 누군가 USB 드라이브를 꽂을 때)를 수신하는 Python 프로그램이 있고 이벤트가 발생하면 메타 데이터를 수집하는 스레드를 만들고 싶습니다 새롭게 연결된 장치에. 그러나이 작업을 비동기 적으로 수행하려고합니다. 즉, 하나의 스레드가 장치에서 메타 데이터 수집을 유지하면서 부모에게 제어를 반환하는 동안 이러한 이벤트를 계속 수신 할 수 있습니다. 지금은 컬렉션이 완료 될 때까지 스레드가 차단됩니다. 여기에 내 코드의 샘플입니다Python 스레딩 질문 - 부모에게 컨트롤 반환
class DeviceAddedListener:
def __init__(self):
self.bus = dbus.SystemBus()
self.hal_manager_obj = self.bus.get_object("org.freedesktop.Hal", "/org$
self.hal_manager = dbus.Interface(self.hal_manager_obj, "org.freedeskto$
self.hal_manager.connect_to_signal("DeviceAdded", self._filter)
def _filter(self, udi):
device_obj = self.bus.get_object ("org.freedesktop.Hal", udi)
device = dbus.Interface(device_obj, "org.freedesktop.Hal.Device")
if device.QueryCapability("volume"):
return self.capture(device)
def capture(self,volume):
self.device_file = volume.GetProperty("block.device")
self.label = volume.GetProperty("volume.label")
self.fstype = volume.GetProperty("volume.fstype")
self.mounted = volume.GetProperty("volume.is_mounted")
self.mount_point = volume.GetProperty("volume.mount_point")
try:
self.size = volume.GetProperty("volume.size")
except:
self.size = 0
print "New storage device detected:"
print " device_file: %s" % self.device_file
print " label: %s" % self.label
print " fstype: %s" % self.fstype
if self.mounted:
print " mount_point: %s" % self.mount_point
response = raw_input("\nWould you like to acquire %s [y/N]? " % self.device_file)
if (response == "y"):
self.get_meta()
thread.start_new_thread(DoSomething(self.device_file))
else:
print "Returning to idle"
if __name__ == '__main__':
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
loop = gobject.MainLoop()
DeviceAddedListener()
loop.run()
어떤 생각이 크게 감사하겠습니다 :) 나는 공간을 절약하기 위해 수입 목록을 제외한는
비트를 추출 할 때 샘플 코드를 탐색하는 것이 훨씬 쉽습니다. –