使用 Python Asyncore 构建 SNMP 请求-响应服务
我有一个基于 asyncore 构建的第 3 方协议模块 (SNMP)。 asyncore接口用于处理响应消息。在 asyncore 主循环运行时,设计生成协议请求端的客户端的正确技术是什么。我现在可以想到两个选项:
使用asyncore.loop()的循环、超时参数让我的客户端程序有时间发送适当的请求。
创建一个客户端异步调度程序,它将在与接收方相同的异步处理循环中执行。
创建一个客户
最好的选择是什么?我正在研究第二个解决方案,因为协议 API 不允许我直接访问 asyncore 参数。如果我误解了使用 asyncore 的正确技术,请纠正我。
I have a 3rd-party protocol module (SNMP) that is built on top of asyncore. The asyncore interface is used to process response messages. What is the proper technique to design a client that generate the request-side of the protocol, while the asyncore main loop is running. I can think of two options right now:
Use the loop,timeout parameters of asyncore.loop() to allow my client program time to send the appropriate request.
Create a client asyncore dispatcher that will be executed in the same asyncore processing loop as the receiver.
What is the best option? I'm working on the 2nd solution, cause the protocol API does not give me direct access to the asyncore parameters. Please correct me if I've misunderstood the proper technique for utilizing asyncore.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过在接收进程的 asyncore 循环中添加回调函数来解决这个问题。
该解决方案在某种程度上特定于我正在试验的模块(pySNMP),但总体思路如下:
定义一个函数闭包,该函数闭包返回一个可调用方法,其中存储了对
dict
的引用,并且窗口
变量。dict
跟踪预期的响应,window
是发送者缓冲区的大小。将对闭包函数的引用传递到自定义的 asyncore.dispatcher 实例中。回调函数可以在
writeable
方法调用中执行。将调度程序的超时设置为一个较小的值。这可以防止 asyncore 在等待接收到的数据包时阻塞太长时间。我用了 0.05 秒。越低,应用程序的响应就越大,但不要太低。
更新 asyncore read_handle 方法以从全局
dict
结构中删除收到的响应。这将允许传输新消息。现在启动调度程序和 asyncore 的每个循环,系统将调用回调函数,并发送任何消息,直到定义的窗口大小。
I solved this by adding a callback function into the asyncore loop for the receiver process.
The solution was somewhat specific to the module I was experiment with (pySNMP), but here is the general idea:
define a function closure that returns a callable method with a stored reference to a
dict
andwindow
variable. Thedict
tracks the expected responses, and thewindow
is the size of the sender buffer.pass a reference to the closure function into a customized asyncore.dispatcher instance. The callback function can be executed in the
writeable
method invocation.set the timeout of the dispatcher to a small value. This prevents asyncore from blocking for too long, while waiting for received packets. I used .05 seconds. The lower you go, the more response your app is, but don't go too low.
update the asyncore read_handle method to remove the received responses from your global
dict
structure. This will allow new messages to be transmitted.now kick-off the dispatcher and every loop of the asyncore, the system will call the callback function, and send any messages, up to the defined window size.