免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4664 | 回复: 2
打印 上一主题 下一主题

江湖救急, nginx+webpy+fastcgi模式, 多线程模式会服务挂起不响应 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-04-14 10:36 |只看该作者 |倒序浏览
本帖最后由 isnowran 于 2015-04-15 17:45 编辑

这个问题折腾了好久, 都想换框架了, 但webpy实在用顺手了, 请众位大神帮忙看看
python fastcgi 代码:

  1. #!/usr/local/bin/python2.7

  2. import web

  3. class version(object):
  4.     def GET(self, v):
  5.         print "in"
  6.         return "0.1.2"

  7. urls = ('/(.*)', 'version')

  8. if __name__ == "__main__":
  9.     web.config.debug = False
  10.     app = web.application(urls, globals())
  11.     app.run()
复制代码
运行

  1. [binary@APP src]$ ./run.py fcgi 9002
复制代码
nginx检查:

  1. [root@APP ~]# nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
复制代码
nginx配置:

  1. [root@APP ~]# cat /usr/local/nginx/conf/nginx.conf
  2. worker_processes  1;

  3. events {
  4.     worker_connections  1024;
  5.     use epoll;
  6. }

  7. http {
  8.     include       mime.types;
  9.     default_type  application/octet-stream;

  10.     sendfile        on;

  11.     keepalive_timeout  65;

  12.     server {
  13.         listen       80;
  14.         server_name  localhost;

  15.         location / {
  16.             root   html;
  17.             index  index.html index.htm;
  18.             include fastcgi_params;
  19.                 fastcgi_param REQUEST_METHOD $request_method;
  20.                 fastcgi_param QUERY_STRING $query_string;
  21.                 fastcgi_param CONTENT_TYPE $content_type;
  22.                 fastcgi_param CONTENT_LENGTH $content_length;
  23.                 fastcgi_param GATEWAY_INTERFACE CGI/1.1;
  24.                 fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
  25.                 fastcgi_param REMOTE_ADDR $remote_addr;
  26.                 fastcgi_param REMOTE_PORT $remote_port;
  27.                 fastcgi_param SERVER_ADDR $server_addr;
  28.                 fastcgi_param SERVER_PORT $server_port;
  29.                 fastcgi_param SERVER_NAME $server_name;
  30.                 fastcgi_param SERVER_PROTOCOL $server_protocol;
  31.                 fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
  32.                 fastcgi_param PATH_INFO $fastcgi_script_name;
  33.                 fastcgi_pass 127.0.0.1:9002;
  34.         }

  35.         error_page   500 502 503 504  /50x.html;
  36.         location = /50x.html {
  37.             root   html;
  38.         }
  39.     }
  40. }
  41. [root@APP ~]#
复制代码
第一次请求测试:

  1. [root@APP ~]# curl http://127.0.0.1/
  2. 0.1.2
复制代码
ab1000个并发测试:

  1. [root@APP ~]# ab -c1000 -n 1000 http://127.0.0.1/      
  2. This is ApacheBench, Version 2.3 <$Revision: 655654 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/

  5. Benchmarking 127.0.0.1 (be patient)
  6. apr_socket_recv: Connection reset by peer (104)
  7. Total of 32 requests completed
复制代码
再次请求测试, 就卡死了, 但是, fcgi能打印出"in":

  1. [root@APP ~]# curl http://127.0.0.1/
复制代码
重启nginx没用, 必须重启fastcgi

为了排除是nginx的问题, 我又用c做fastcgi测试, 就没有问题, 无论并发有多大, 都不会造成无响应, 代码如下:

  1. [binary@APP src]$ cat cgi.cpp
  2. #include <iostream>
  3. #include <fcgi_stdio.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>

  6. using namespace std;

  7. int main ()
  8. {
  9.         int count = 0;

  10.         while (FCGI_Accept () >= 0)
  11.         {

  12.                 printf ("Content-type:text/html\r\n\r\n");
  13.                 printf ("<p> Hello FastCGI !  </ p>");
  14.                 printf ("<br /> Request number = [%d]", ++count);
  15.                 printf ("<br /> Process ID: %d ", getpid ());
  16.         }

  17.         return 0;
  18. }
复制代码
各个组件版本, nginx:1.63. webpy:0.3.7:

  1. [binary@APP src]$ nginx -v  
  2. nginx version: nginx/1.6.3
  3. [binary@APP src]$
复制代码

  1. NAME
  2.     web - web.py: makes web apps (http://webpy.org)

  3. FILE
  4.     /usr/local/lib/python2.7/site-packages/web/__init__.py

  5. PACKAGE CONTENTS
  6.     application
  7.     browser
  8.     contrib (package)
  9.     db
  10.     debugerror
  11.     form
  12.     http
  13.     httpserver
  14.     net
  15.     python23
  16.     session
  17.     template
  18.     test
  19.     utils
  20.     webapi
  21.     webopenid
  22.     wsgi
  23.     wsgiserver (package)

  24. SUBMODULES
  25.     __init__
  26.     web

  27. DATA
  28.     __author__ = ['Aaron Swartz <me@aaronsw.com>', 'Anand Chitipothu <anan...
  29.     __contributors__ = 'see http://webpy.org/changes'
  30.     __license__ = 'public domain'
  31.     __version__ = '0.37'
  32.     config = <Storage {'debug': True, '__doc__': '\nA configu...A0A0J', 'h...
  33.     ctx = <ThreadedDict {'__doc__': '\nA `storage` object ...`\n   : A str...
  34.     generators = _Feature((2, 2, 0, 'alpha', 1), (2, 3, 0, 'final', 0), 0)
  35.     iters = (<type 'list'>, <type 'tuple'>, <type 'set'>, <type 'set'>)
  36.     re_compile = <web.utils.Memoize instance>

  37. VERSION
  38.     0.37

  39. AUTHOR
  40.     ['Aaron Swartz <me@aaronsw.com>', 'Anand Chitipothu <anandology@gmail.com>']
复制代码

论坛徽章:
0
2 [报告]
发表于 2015-04-14 12:36 |只看该作者
本帖最后由 isnowran 于 2015-04-14 12:37 编辑

找到了问题所在, 应该是flup多线程不加锁导致的, 但不知道为啥, 莫非这个参数会导致flup死锁?:
哪位大侠能深究一下, 给兄弟长长眼吧
在 /usr/local/lib/python2.7/site-packages/web/wsgi.py:

  1. def runfcgi(func, addr=('localhost', 8000)):
  2.     """Runs a WSGI function as a FastCGI server."""
  3.     import flup.server.fcgi as flups
  4.     #return flups.WSGIServer(func, multiplexed=True, bindAddress=addr, debug=False).run()
  5.     # 改为 multiplexed = False
  6.     return flups.WSGIServer(func, multiplexed=False, bindAddress=addr, debug=False).run()
复制代码
影响到的flup代码:
vi /usr/local/lib/python2.7/site-packages/flup/server/fcgi_base.py

  1.         if not self.multithreaded:
  2.             self._appLock.acquire()
  3.         try:
  4.             try:
  5.                 result = self.application(environ, start_response)
  6.                 try:
  7.                     for data in result:
  8.                         if data:
  9.                             write(data)
  10.                     if not headers_sent:
  11.                         write('') # in case body was empty
  12.                 finally:
  13.                     if hasattr(result, 'close'):
  14.                         result.close()
  15.             except socket.error, e:
  16.                 if e[0] != errno.EPIPE:
  17.                     raise # Don't let EPIPE propagate beyond server
  18.         finally:
  19.             if not self.multithreaded:
  20.                 self._appLock.release()
复制代码

论坛徽章:
0
3 [报告]
发表于 2015-04-24 15:02 |只看该作者
我来学习了!!好帖,支持下!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP