2015-11-03 48 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) 
相关问题