(也欢迎以更好的形式帮助您解决此问题)
我正在寻找一种实现与互联网完全无关的异步软件的工具.我还获得了一份Twisted Network Programming Essentials的副本,该副本非常令人失望.它以及几乎所有其他教程似乎都只将twist视为网络客户端,使用内置事件处理程序,而静默使用内置胶水代码,这很难一概而论.
对于上下文,我的应用程序希望使用串行/ pyserial与硬件设备进行对话,并使用Qt提供gui.我什至还没有开始担心Qt反应器(它似乎是蠕虫罐头的另一个承诺)或移植到Windows的地步.
首先,我使用的是扭曲选择反应堆,在其中添加了处理udev事件的Protocol FileDescriptor.到目前为止,我的工作原理是,udev事件触发了协议中的功能(eventReceived).
以下是协议及其添加到反应堆的方式:
class UdevMonitorListener(Protocol):
def __init__(self, _reactor=None):
if _reactor is not None:
self._reactor = _reactor
else:
self._reactor = reactor
self.subsystem = 'tty'
self.monitor = None
def startListening(self):
logger.info("Starting UdevMonitorListener")
self.monitor = UdevMonitor(self._reactor, self, self.subsystem)
self.monitor.startReading()
def eventReceived(self, action, device):
if device in connected_devices.udev_ports:
if action == u'remove':
connected_devices.remove_by_udev_port(device)
if action == u'add':
if is_device_supported_from_udev_port(device):
if device not in connected_devices.udev_ports:
connected_devices.append_by_udev_port(device)
def init(_reactor=None):
monitor_protocol = UdevMonitorListener(_reactor)
monitor_protocol.startListening()
函数init()由Reactor.run()之前的Reactor.callWhenRunning()调用.如文件描述符所预期的那样,调用了eventReceived函数.如果有帮助,我也可以在此处添加该代码.
我想要的是eventReceiveved触发反应堆中的某种事件,其他事件可以对此做出反应.这段代码不必关心谁在使用它,而该代码也不应该在乎谁在生成它.这些事件之间的距离不远,而且我似乎找不到能够干净地执行此操作的接口.预计这些事件不会经常发生,但是它们永远不会“结束”.如果要使用延迟,则它必须具有某种“刷新”自身的方式以等待下一个事件.处理此类事件的常用模式是什么?
编辑:
为了后代和其他任何人看,其余代码:
class UdevMonitor(FileDescriptor):
"""
File Descriptor for pyudev.Monitor.
@see: U{http://packages.python.org/pyudev/api/monitor.html}.
"""
def __init__(self, _reactor, protocol, subsystem=None):
FileDescriptor.__init__(self, _reactor)
# Set up monitor
context = pyudev.Context()
self.monitor = pyudev.Monitor.from_netlink(context)
if subsystem:
self.monitor.filter_by(subsystem=subsystem)
# Connect protocol
assert isinstance(protocol, UdevMonitorListener)
self.protocol = protocol
self.protocol.makeConnection(self)
def fileno(self):
"""
Return monitor's file descriptor.
"""
return self.monitor.fileno()
def startReading(self):
"""
Start waiting for read availability.
"""
logger.debug("starting udev monitor fd")
self.monitor.start()
FileDescriptor.startReading(self)
def doRead(self):
"""
An event is ready, decode it through Monitor and call our protocol.
"""
logger.debug("udev reports event available")
event = self.monitor.receive_device()
if event:
action, device = event
self.protocol.eventReceived(action, device)
def writeSomeData(self, data):
raise IOError("You can't write to a udev Monitor")