我还没找到异步处理的方式。如果有异步的实现方式,那就可以单线程异步处理多个并发请求了。
不过我在FastCGI的sample里面找到了多线程的方式,多线程的方式封装一下应该也可以达到异步的效果的。比如,doit()线程把FCGX_Request request丢给另一个线程A处理,然后doit()线程阻塞的等待线程A的返回结果。那么线程A实际上就可以采取异步的方式来处理请求了。
以下是FastCGI的sample里面的多线程实现:
/* * threaded.c -- A simple multi-threaded FastCGI application. */#ifndef lintstatic const char rcsid[] = "$Id: threaded.c,v 1.9 2001/11/20 03:23:21 robs Exp $";#endif /* not lint */#include "fcgi_config.h"#include#include #ifdef HAVE_UNISTD_H#include #endif#include "fcgiapp.h"#define THREAD_COUNT 20static int counts[THREAD_COUNT];static void *doit(void *a){ int rc, i, thread_id = (int)a; pid_t pid = getpid(); FCGX_Request request; char *server_name; FCGX_InitRequest(&request, 0, 0); for (;;) { static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t counts_mutex = PTHREAD_MUTEX_INITIALIZER; /* Some platforms require accept() serialization, some don't.. */ pthread_mutex_lock(&accept_mutex); rc = FCGX_Accept_r(&request); pthread_mutex_unlock(&accept_mutex); if (rc < 0) break; server_name = FCGX_GetParam("SERVER_NAME", request.envp); FCGX_FPrintF(request.out, "Content-type: text/html " " " " FastCGI Hello! (multi-threaded C, fcgiapp library) " "FastCGI Hello! (multi-threaded C, fcgiapp library)
" "Thread %d, Process %ld" "Request counts for %d threads running on host %s
", thread_id, pid, THREAD_COUNT, server_name ? server_name : "?"); sleep(2); pthread_mutex_lock(&counts_mutex); ++counts[thread_id]; for (i = 0; i < THREAD_COUNT; i++) FCGX_FPrintF(request.out, "%5d " , counts[i]); pthread_mutex_unlock(&counts_mutex); FCGX_Finish_r(&request); } return NULL;}int main(void){ int i; pthread_t id[THREAD_COUNT]; FCGX_Init(); for (i = 1; i < THREAD_COUNT; i++) pthread_create(&id[i], NULL, doit, (void*)i); doit(0); return 0;}
另外,FCGX_Accept_r()可以在FCGX_Finish_r()之前连续accept多个request,这应该也代表着一种异步的方法。不过需要构造多个request对象给FCGX_Accept_r()。转自:http://blog.csdn.net/wwwsq