Friday, April 18, 2008

asyncore v.s. twisted

There are several module/packages available when you make a server-client program.
asyncore is a Python standard module, i.e. when you install Python on your machine, you can use it immediately. Twisted is an open source project. They are both for server-client programs.

While I was making pyCommandPort, I tried not to use twisted, because it's just a small utility plug-in and twisted is too much overkill. I didn't want the force the users to install twisted on their machines, instead I used asyncore. I could do low level socket programming/multi thread programming but it was another bothering and time consuming work.

Actually asyncore and twisted are quite similar. Both of them have a single main dispatcher and user functions are called by the dispathcer. While asyncore is for only server-client type programs, twisted is for any programming that deals with blocking behavior i.e. behavior that needs to wait for some slow processes. Though twisted is typically used for network programming because server/client communication is a typical slow response, twisted's usage is not limited for that purpose.

One example is "Maya execution". I used MGlobal::executePythonCommandOnIdle() API. When it is called with a Python script as a parameter, Maya registers it in the internal queue of Python scripts, and when Maya had done everything (rendering, subdividing polygon, ...), it executes the queued Python scripts one by one, in the main thread. This is not Maya specific function, many framework has it, e.g. wxPython has equivalent function. wx.CallAfter() is the one. When you call MGlobal::executePythonCommandOnIdle(), you can never see when it is executed. It may be executed immediately, or tomorrow. I wanted to send the error message to the client of pyCommandPort server, and it is another type of 'slow response' behavior. Since asyncore is only for network programming, it cannot handle these problems nicely.
If it had been something like querying something to the database server instead of Maya execution, asyncore can handle it because accessing to a database server is a network programming, but in this case not. asyncore tries to do everything in the main thread, and the main thread cannot wait for Maya execution at the same time waiting for another incoming connection.

Twisted is much more flexible. I will write what a twisted programming is like on another post soon.

1 comment:

Sumit said...

Hi Koichi!
Thanks for sharing.. and clarifying my doubts. I am trying to refactor a blocking portion of a command line application, but most resources are pointing twisted towards network programming only.