【问题标题】:Running a server as a standalone process - Python将服务器作为独立进程运行 - Python
【发布时间】:2016-02-04 04:40:54
【问题描述】:

我正在尝试在我的测试框架中让网络服务器作为独立进程运行:

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: */*

但是一旦任务重复,网络服务器就会停止运行:

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 服务器向终端输出大量信息。 我该如何解决?网络服务器停止运行的原因是什么?

【问题讨论】:

    标签: python multithreading process operating-system


    【解决方案1】:

    使用stdin=PIPE, stdout=PIPE, stderr=PIPE,您实际上是在创建管道。一旦他们的缓冲区满了,他们就会阻塞。那时,服务器将永远等待您的主进程读取它们。如果您不需要输出,只需执行devnull = open(os.devnull, 'r+') 然后stdin=devnull, ...。参数close_fds=True 不会关闭标准输入、标准输出和标准错误。简而言之:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-07
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 2021-08-16
      相关资源
      最近更新 更多