2013-09-06 2 views
3

안녕하세요. 저는 fastcgi 응용 프로그램을 만들려고 노력해 왔습니다. 그리고 한 번에 많은 요청을 처리 할 수 ​​있도록 다중 스레드되기를 바랍니다. 나는 코드를 발견하고,이 코드는 요청이 완전히 완료 될 때까지, 그래서 실제로는 대기 동의에 stucking을 유지입니다Nginx + fastcgi multithreading

FCGX_InitRequest(&request, 0, FCGI_FAIL_ACCEPT_ON_INTR); 

for (;;) 
{ 
    static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER; 
    static pthread_mutex_t counts_mutex = PTHREAD_MUTEX_INITIALIZER; 
    pthread_mutex_lock(&accept_mutex); 
    rc = FCGX_Accept_r(&request); 
    pthread_mutex_unlock(&accept_mutex); 

    ... ... ... 


    FCGX_FPrintF(request.out,"%s",test_stream.str().c_str()); 

    FCGX_Finish_r(&request); 
} 

그것을 조금 수정했습니다.

나는 문제가 해결이

spawn-fcgi -p 8001 -U www-data -n handler.cgi -F 32 
spawn-fcgi -p 8001 -U www-data -n handler.cgi -- /usr/bin/multiwatch -F 32 

답변

2

처럼 산란 FastCGI를 시도했다. 소켓을 허용하려면 FCGX_OpenSocket 및 umask (0)를 사용하십시오. 이렇게하면 실제 다중 스레드 응용 프로그램을 제공 할 것입니다.

#include <pthread.h> 
#include <sys/types.h> 
#include <stdio.h> 

#include "fcgi_config.h" 
#include "fcgiapp.h" 


#define THREAD_COUNT 8 
#define SOCKET_PATH "/var/run/myfcgiserver.sock" // your unix socket file 

static int socketId; 

static void *doit(void *a) 
{ 
    int rc, i; 
    FCGX_Request request; 
    char *server_name; 

    if(FCGX_InitRequest(&request, socketId, 0) != 0) 
    { 
     printf("Can not init request\n"); 
     return NULL; 
    } 
    printf("Request is inited\n"); 

    for(;;) 
    { 
     static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER; 

     printf("Try to accept new request\n"); 
     pthread_mutex_lock(&accept_mutex); 
     rc = FCGX_Accept_r(&request); 
     pthread_mutex_unlock(&accept_mutex); 

     if(rc < 0) 
     { 
      printf("Can not accept new request\n"); 
      break; 
     } 
     printf("request is accepted\n"); 

     server_name = FCGX_GetParam("SERVER_NAME", request.envp); 

     FCGX_PutS("Content-type: text/html\r\n", request.out); 
     FCGX_PutS("\r\n", request.out); 
     FCGX_PutS("<html>\r\n", request.out); 
     FCGX_PutS("<head>\r\n", request.out); 
     FCGX_PutS("<title>FastCGI Hello! (multi-threaded C, fcgiapp library)</title>\r\n", request.out); 
     FCGX_PutS("</head>\r\n", request.out); 
     FCGX_PutS("<body>\r\n", request.out); 
     FCGX_PutS("<h1>FastCGI Hello! (multi-threaded C, fcgiapp library)</h1>\r\n", request.out); 
     FCGX_PutS("<p>Request accepted from host <i>", request.out); 
     FCGX_PutS(server_name ? server_name : "?", request.out); 
     FCGX_PutS("</i></p>\r\n", request.out); 
     FCGX_PutS("</body>\r\n", request.out); 
     FCGX_PutS("</html>\r\n", request.out); 

     FCGX_Finish_r(&request); 

    } 

    return NULL; 
} 

int main(void) 
{ 
    int i; 
    pthread_t id[THREAD_COUNT]; 

    FCGX_Init(); 
    printf("Lib is inited\n"); 
    umask(0); 
    socketId = FCGX_OpenSocket(SOCKET_PATH, 2000); 
    if(socketId < 0) 
    { 
      return 1; 
    } 
    printf("Socket is opened\n"); 


    for(i = 0; i < THREAD_COUNT; i++) 
    { 
     pthread_create(&id[i], NULL, doit, NULL); 
    } 

    for(i = 0; i < THREAD_COUNT; i++) 
    { 
     pthread_join(id[i], NULL); 
    } 

    return 0; 
} 
+0

샘플 nginx.conf 파일을 추가 할 수 있습니까? – YasserAsmi

관련 문제