使用 Python Asyncore 构建 SNMP 请求-响应服务

发布于 2024-08-28 10:42:27 字数 337 浏览 5 评论 0原文

我有一个基于 asyncore 构建的第 3 方协议模块 (SNMP)。 asyncore接口用于处理响应消息。在 asyncore 主循环运行时,设计生成协议请求端的客户端的正确技术是什么。我现在可以想到两个选项:

  1. 使用asyncore.loop()的循环、超时参数让我的客户端程序有时间发送适当的请求。

  2. 创建一个客户端异步调度程序,它将在与接收方相同的异步处理循环中执行。

    创建一个客户

最好的选择是什么?我正在研究第二个解决方案,因为协议 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:

  1. Use the loop,timeout parameters of asyncore.loop() to allow my client program time to send the appropriate request.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心荒芜 2024-09-04 10:42:27

我通过在接收进程的 asyncore 循环中添加回调函数来解决这个问题。

该解决方案在某种程度上特定于我正在试验的模块(pySNMP),但总体思路如下:

  1. 定义一个函数闭包,该函数闭包返回一个可调用方法,其中存储了对dict的引用,并且窗口变量。 dict 跟踪预期的响应,window 是发送者缓冲区的大小。

  2. 将对闭包函数的引用传递到自定义的 asyncore.dispatcher 实例中。回调函数可以在writeable方法调用中执行。

  3. 将调度程序的超时设置为一个较小的值。这可以防止 asyncore 在等待接收到的数据包时阻塞太长时间。我用了 0.05 秒。越低,应用程序的响应就越大,但不要太低。

  4. 更新 asyncore read_handle 方法以从全局 dict 结构中删除收到的响应。这将允许传输新消息。

  5. 现在启动调度程序和 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:

  1. define a function closure that returns a callable method with a stored reference to a dict and window variable. The dict tracks the expected responses, and the window is the size of the sender buffer.

  2. pass a reference to the closure function into a customized asyncore.dispatcher instance. The callback function can be executed in the writeable method invocation.

  3. 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.

  4. update the asyncore read_handle method to remove the received responses from your global dict structure. This will allow new messages to be transmitted.

  5. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文