views:

64

answers:

1

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.

A: 

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.

Casey