2016-08-25 75 views
0

我有一個Python Flask應用程序正在與nginxuwsgi服務。我在本地機器上開發應用程序,當我在瀏覽器中打開它時,一切都很好。我發出一個POST請求,請求返回正常。到目前爲止這麼好...nginx uwsgi不會接受新的連接,而請求正在處理中

現在這個POST請求是一個長時間運行的請求,計算量非常大,大概需要60秒才能運行。所以我想測試是否可以打開多個連接。我發出POST請求,然後在另一個瀏覽器選項卡中打開該應用程序,但在POST請求已被響應之前它不會加載。

我對nginx和uwsgi非常陌生,甚至到了這一步還很艱難,但我認爲這個想法是您可以更有效地處理連接和開箱即裝,因此,我認爲我在這裏犯了一個菜鳥的錯誤。

我怎樣才能讓這個應用程序處理多個連接和請求?

這裏是我的nginx.conf

daemon off; 

events { 
    worker_connections 1024; 
} 

http { 
    server { 
     listen  80; 
     server_name 127.0.0.1; 

     #root /app; 

     charset UTF-8; 
     access_log /var/log/nginx/t206cv.access.log; 

     location/{ 
      proxy_pass http://app; 
      proxy_connect_timeout  600; 
      proxy_send_timeout   600; 
      proxy_read_timeout   600; 
      send_timeout    600; 
     } 
    } 

    upstream app { 
     server app:5000; 
    } 
} 

這裏是我的uwsgi.ini

[uwsgi] 
chdir = /app 
module = t206cv:app 
http-socket = 0.0.0.0:5000 
master = True 

這裏是當我啓動應用程序會發生什麼:

app_1 | [uWSGI] getting INI configuration from /etc/uwsgi.ini 
app_1 | *** Starting uWSGI 2.0.13.1 (64bit) on [Thu Aug 25 00:49:37 2016] *** 
app_1 | compiled with version: 4.9.2 on 24 August 2016 02:00:22 
app_1 | os: Linux-4.1.19-boot2docker #1 SMP Mon Mar 7 17:44:33 UTC 2016 
app_1 | nodename: b6faafc928a1 
app_1 | machine: x86_64 
app_1 | clock source: unix 
app_1 | pcre jit disabled 
app_1 | detected number of CPU cores: 1 
app_1 | current working directory:/
app_1 | detected binary path: /usr/local/bin/uwsgi 
app_1 | uWSGI running as root, you can use --uid/--gid/--chroot options 
app_1 | *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
app_1 | chdir() to /app 
app_1 | your processes number limit is 1048576 
app_1 | your memory page size is 4096 bytes 
app_1 | detected max file descriptor number: 1048576 
app_1 | lock engine: pthread robust mutexes 
app_1 | thunder lock: disabled (you can enable it with --thunder-lock) 
app_1 | uwsgi socket 0 bound to TCP address 0.0.0.0:5000 fd 3 
app_1 | Python version: 2.7.12 (default, Aug 22 2016, 20:25:04) [GCC 4.9.2] 
app_1 | *** Python threads support is disabled. You can enable it with --enable-threads *** 
app_1 | Python main interpreter initialized at 0xe34450 
app_1 | your server socket listen backlog is limited to 100 connections 
app_1 | your mercy for graceful operations on workers is 60 seconds 
app_1 | mapped 145536 bytes (142 KB) for 1 cores 
app_1 | *** Operational MODE: single process *** 
app_1 | WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0xe34450 pid: 6 (default app) 
app_1 | *** uWSGI is running in multiple interpreter mode *** 
app_1 | spawned uWSGI master process (pid: 6) 
app_1 | spawned uWSGI worker 1 (pid: 11, cores: 1) 
app_1 | [pid: 11|app: 0|req: 1/1] 172.17.0.3() {34 vars in 605 bytes} [Thu Aug 25 00:50:06 2016] GET/=> generated 2383 bytes in 16 msecs (HTTP/1.0 200) 2 headers in 81 bytes (1 switches on core 0) 
app_1 | [pid: 11|app: 0|req: 2/2] 172.17.0.3() {38 vars in 697 bytes} [Thu Aug 25 00:50:06 2016] GET /static/styles.css => generated 0 bytes in 6 msecs (HTTP/1.0 304) 4 headers in 181 bytes (0 switches on core 0) 
app_1 | [pid: 11|app: 0|req: 3/3] 172.17.0.3() {38 vars in 683 bytes} [Thu Aug 25 00:50:06 2016] GET /static/scripts.js => generated 0 bytes in 1 msecs (HTTP/1.0 304) 4 headers in 182 bytes (0 switches on core 0) 
app_1 | [pid: 11|app: 0|req: 4/4] 172.17.0.3() {34 vars in 605 bytes} [Thu Aug 25 00:51:03 2016] GET/=> generated 2383 bytes in 2 msecs (HTTP/1.0 200) 2 headers in 81 bytes (1 switches on core 0) 
app_1 | [pid: 11|app: 0|req: 5/5] 172.17.0.3() {38 vars in 697 bytes} [Thu Aug 25 00:51:03 2016] GET /static/styles.css => generated 0 bytes in 2 msecs (HTTP/1.0 304) 4 headers in 181 bytes (0 switches on core 0) 
app_1 | [pid: 11|app: 0|req: 6/6] 172.17.0.3() {38 vars in 683 bytes} [Thu Aug 25 00:51:03 2016] GET /static/scripts.js => generated 0 bytes in 3 msecs (HTTP/1.0 304) 4 headers in 182 bytes (0 switches on core 0) 
app_1 | [pid: 11|app: 0|req: 7/7] 172.17.0.3() {40 vars in 683 bytes} [Thu Aug 25 00:51:06 2016] POST /search => generated 89 bytes in 81585 msecs (HTTP/1.0 200) 2 headers in 71 bytes (2 switches on core 0) 
app_1 | [pid: 11|app: 0|req: 8/8] 172.17.0.3() {32 vars in 574 bytes} [Thu Aug 25 00:52:28 2016] GET/=> generated 2383 bytes in 1 msecs (HTTP/1.0 200) 2 headers in 81 bytes (1 switches on core 0) 
app_1 | [pid: 11|app: 0|req: 9/9] 172.17.0.3() {32 vars in 574 bytes} [Thu Aug 25 00:52:28 2016] GET/=> generated 2383 bytes in 2 msecs (HTTP/1.0 200) 2 headers in 81 bytes (1 switches on core 0) 

回答

0

你只運行一名工人。

spawned uWSGI worker 1 (pid: 11, cores: 1) 

一個線程/進程worker可以處理一個請求。在uWSGI配置中配置更多工作人員。

workers = 8 

使用cheaper mode一種簡單的方式來擴展基於需求的工作進程。

cheaper = 2 
cheaper-initial = 2 
workers = 16 
相關問題