2015-11-03 47 views
0

我想有作爲運行我的測試框架內的獨立進程web服務器:運行一臺服務器作爲一個獨立的過程 - 的Python

from subprocess import Popen, PIPE, STDOUT 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.support.ui import Select 
from selenium.webdriver import ActionChains 
from selenium.webdriver.common.by import By 

server_cmd = "bundle exec thin start -p 3001 --ssl" # rails comamnd 
# intend to start the server as a standalone process 
webserver = Popen(server_cmd, shell=True, stdin=PIPE, stdout=PIPE, 
        stderr=PIPE, close_fds=True) 

的服務器工作出很好的話,我執行一些任務Selenium。在第一時間,這些任務執行很好:

curl -v https://localhost:3001 -k 
* Rebuilt URL to: https://localhost:3001/ 
* Trying 127.0.0.1... 
* Connected to localhost (127.0.0.1) port 3001 (#0) 
* TLS 1.0 connection using TLS_RSA_WITH_AES_256_CBC_SHA 
* Server certificate: openca.steamheat.net 
> GET/HTTP/1.1 
> Host: localhost:3001 
> User-Agent: curl/7.43.0 
> Accept: */* 

然而,一旦任務是重複的,Web服務器停止運行:

curl -v https://localhost:3001 -k -L 
* Rebuilt URL to: https://localhost:3001/ 
* Trying 127.0.0.1... 
* Connected to localhost (127.0.0.1) port 3001 (#0) 
* Closing connection 0 

當我執行,在一個shell終端相同的命令,這兩個任務完成如所假設的。 我想知道是否與stdout的輸出量有關,因爲Rails服務器向終端輸出大量信息。 我該如何解決這個問題?網絡服務器停止運行的原因是什麼?

回答

1

隨着stdin=PIPE, stdout=PIPE, stderr=PIPE你基本上正在創建管道。一旦他們的緩衝區滿了,他們會阻止。此時,服務器將永久等待主進程讀取它們。如果你不需要輸出簡單地做devnull = open(os.devnull, 'r+')然後stdin=devnull, ...。參數close_fds=True不會關閉stdin,stdout和stderr。總之:

import os 
devnull = open(os.devnull, 'r+') 
webserver = Popen(server_cmd, shell=True, stdin=devnull, 
        stdout=devnull, stderr=devnull, close_fds=True) 
相關問題