简体   繁体   English

如何最好地部署用C编写的Web应用程序?

[英]How can I best deploy a web application written in C?

Say I have fancy new algorithm written in C, 假设我有用C编写的新算法,

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

And I want to deploy as a web application, for example at 我想部署为Web应用程序,例如在

http://example.com/addone?a=5 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. 我有一个在Apache2上使用Python mod_wsgi的现有设置,并且为了进行测试,我刚刚从C构建了一个二进制文件,并使用Python的os.popen2作为子进程进行调用。

I want this to be very fast and not waste overhead (ie I don't need this other Python stuff at all). 我希望它非常快并且不浪费开销(即我根本不需要其他Python东西)。 I can dedicate the whole server to it, re-compile anything needed, etc. 我可以将整个服务器专用于此,重新编译所需的任何东西,等等。

I'm thinking about looking into Apache C modules. 我正在考虑研究Apache C模块。 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. 或者,我可以构建SWIG包装器以直接从Python调用,但是如果我根本不使用Python,那似乎又很浪费。 Any tips? 有小费吗?

The easiest way should be to write this program as a CGI app ( http://en.wikipedia.org/wiki/.cgi ). 最简单的方法应该是将该程序编写为CGI应用程序( http://en.wikipedia.org/wiki/.cgi )。 It would run with any webserver that supports the Common Gateway Interface. 它可以与支持通用网关接口的任何Web服务器一起运行。 The output format needs to follow the CGI rules. 输出格式需要遵循CGI规则。

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. 如果要充分利用Web服务器功能,则可以用C编写Apache模块。这需要更多准备,但可以完全控制。

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. 也许这种与C语言一起使用的小型动态Web服务器可以帮助您..它应该易于使用且自成一体。

Probably the fastest solution you can adopt according to the benchmarks shown on their homepage! 可能是根据其首页上显示的基准可以采用的最快解决方案!

This article from yesterday has a good discussion on why not to use C as a web framework. 昨天的这篇文章对为什么不使用C作为Web框架进行了很好的讨论。 I think an easy solution for you would be to use ctypes, it's certainly faster than starting a subprocess. 我认为对您来说,一个简单的解决方案是使用ctypes,它肯定比启动子进程快。 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))

I have just setup a web service using libmicrohttpd and have had amazing results. 我刚刚使用libmicrohttpd设置了一个Web服务,并取得了惊人的效果。 On a quad core I've been handling 20400 requests a second and the CPU is running only at 58%. 在四核上,我每秒处理20400个请求,而CPU仅以58%的速度运行。 This is probably going to be deployed on a server with 8 cores, so I'm expecting much better results. 这可能将被部署在具有8个核心的服务器上,因此我期望会有更好的结果。 A very simple C service will be even faster! 一个非常简单的C服务将更快!

I have tried GWAN, it is very good, but it's closed, and doesn't play well with virtual environments. 我尝试过GWAN,它非常好,但是它是封闭的,在虚拟环境中不能很好地工作。 I will give @Gil kudos being good at supporting it here though. 我会在这里给@Gil kudos很好的支持。 We just had a few issues and found LibMicroHttpd works better for our needs. 我们只有几个问题,发现LibMicroHttpd可以更好地满足我们的需求。

If you go here, you may need to update your openssl if you're using CentOs from axivo 如果您去这里,如果您使用的是来自axivo的 CentO,则可能需要更新openssl

rpm -ivh --nosignature http://rpm.axivo.com/redhat/axivo-release-6-1.noarch.rpm
yum --disablerepo=* --enablerepo=axivo update openssl-devel

您可以尝试Duda I / O,它仅需要Linux主机: http : //duda.io

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. 我并不是说Apache / Python一定是正确的,但是将架构中的关注点由高度专注的元素组成,这是他们在整个系统中的功能专家,这令人信服。

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. 将基于C的算法服务器与HTTP服务器分离,可以使您访问HTTP可伸缩性和缓存设施等内容,否则如果这些元素之间的耦合过于紧密,就必须在算法组件内进行设计(或重新设计)。

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. 例如,带有基于C的驱动程序的YAWS部署可能是一个非常有效的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将用C编写的现有项目与基于iphone视图的应用程序项目集成? - how can I integrate the existing project written in C with the iphone view-based application project? 如何将用 Golang 编写的应用程序作为 Windows 服务运行 - How can I run the application written in Golang as a Windows service 如何编译和运行这个1989年编写的C程序? - How can I compile and run this 1989 written C program? C:如何检查是否已写入 memory 地址? - C: How can I check if a memory address has been written? 如何重新启动用Windows编写的控制台c应用程序? - How to restart a console c application written in windows? 我可以使用用 C 编写的 dll 和 PHP 吗? - Can I use a dll written in C with PHP? 如何使用C语言编写的CGI应用程序中的系统命令在Web浏览器(Apache服务器)上执行批处理脚本 - How to execute a batch script using system command in a CGI application written in C, executed through web browser (Apache Server) 如何用C语言编写linux启动代码? - How can linux boot code be written in C? 如何传递可以从本机写入C#的空字符串缓冲区? - How can I pass an empty string buffer that can be written to from native to c#? 如何在用c编写的Web客户端中获取http状态代码 - how to get http status code in a web client written in c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM