views:

103

answers:

4

Say I have fancy new algorithm written in C,

int addone(int a) {
    return a + 1;
}

And I want to deploy as a web application, for example at

http://example.com/addone?a=5

which responds with,

Content-Type: text/plain

6

What is the best way to host something like this? I have an existing setup using Python mod_wsgi on Apache2, and for testing I've just built a binary from C and call as a subprocess using Python's os.popen2.

I want this to be very fast and not waste overhead (ie I don't need this other Python stuff at all). I can dedicate the whole server to it, re-compile anything needed, etc.

I'm thinking about looking into Apache C modules. Is that useful? Or I may build SWIG wrappers to call directly from Python, but again that seems wasteful if I'm not using Python at all. Any tips?

+2  A: 

Maybe this tiny dynamic webserver in C to be used with C language can help you.. it should be easy to use and self-contained.

Probably the fastest solution you can adopt according to the benchmarks shown on their homepage!

Jack
+3  A: 

The easiest way should be to write this program as a CGI app (http://en.wikipedia.org/wiki/.cgi). It would run with any webserver that supports the Common Gateway Interface. The output format needs to follow the CGI rules.

If you want to take full advantage of the web server capabilities then you can write an Apache module in C. That needs a bit more preparation but gives you full control.

Steffen Roller
A: 

This article from yesterday has a good discussion on why not to use C as a web framework. I think an easy solution for you would be to use ctypes, it's certainly faster than starting a subprocess. You should make sure that your method is thread safe and that you check your input argument.

from ctypes import *
libcompute = CDLL("libcompute.so")
libcompute.addone(int(a))
DiggyF
A: 

I'm not convinced that you're existing general approach might not be the best one. I'm not saying that Apache/Python is necessarily the correct one but there is something compelling about separating the concerns in your architecture being composed of highly focused elements that are specialists in their functions within the overall system.

Having your C-based algorithm server being decoupled from the HTTP server may give you access to things like HTTP scalability and caching facilities that might otherwise have to be in-engineered (or reinvented) within your algorithm component if things are too tightly coupled.

I don't think performance concerns in of themselves are always the best or only reasons when designing an architecture. For example the a YAWS deployment with a C-based driver could be a very performant option.

bjg