python - 为什么 subprocess.Popen 的输出与预期的不一样?

标签 python django subprocess

我有一个 Django 服务器,它试图与 subprocess.Popen 一起运行并将所有日志存储在一个文件中,这是我的代码:

    with open('thefile.log', 'a') as the_file:
        p1 = subprocess.Popen(['python', os.getcwd() + '\\mySite\\manage.py', 'runserver'], stdout=the_file,
                             stderr=the_file, universal_newlines=True)

这是 thefile.log 中的结果:

Watching for file changes with StatReloader
[26/Jul/2019 13:10:05] "GET / HTTP/1.1" 200 16348
[26/Jul/2019 13:10:05] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[26/Jul/2019 13:10:06] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
[26/Jul/2019 13:10:06] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[26/Jul/2019 13:10:06] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
Not Found: /favicon.ico
[26/Jul/2019 13:10:08] "GET /favicon.ico HTTP/1.1" 404 1976
Not Found: /fs
[26/Jul/2019 13:10:11] "GET /fs HTTP/1.1" 404 1949
Performing system checks...

System check identified no issues (0 silenced).
July 26, 2019 - 13:09:44
Django version 2.2.3, using settings 'mySite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

但应该是这样的:

Watching for file changes with StatReloader

Performing system checks...

System check identified no issues (0 silenced).
July 26, 2019 - 13:09:44
Django version 2.2.3, using settings 'mySite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

[26/Jul/2019 13:10:05] "GET / HTTP/1.1" 200 16348
[26/Jul/2019 13:10:05] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[26/Jul/2019 13:10:06] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
[26/Jul/2019 13:10:06] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[26/Jul/2019 13:10:06] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
Not Found: /favicon.ico
[26/Jul/2019 13:10:08] "GET /favicon.ico HTTP/1.1" 404 1976
Not Found: /fs
[26/Jul/2019 13:10:11] "GET /fs HTTP/1.1" 404 1949

事实上我还有其他一些服务,我的另一个问题是如何在当前终端上使用 subprocess.Popen 以正确的方式打印它们的结果?例如:

service1:
      ------
      ------
service2:
      ------
      ------

最佳答案

你不能像那样将 stdoutstderr 路由到同一个文件处理程序。但是,您可以在此处使用技巧将 stderr“路由”到 stdout(或反之亦然),例如:

with open('thefile.log', 'a') as the_file:
    p1 = subprocess.Popen(
        ['python', os.getcwd() + '\\mySite\\manage.py', 'runserver'],
        stdout=the_file,
        <b>stderr=subprocess.STDOUT</b>,
        universal_newlines=True
    )

这看起来类似于您在其中编写 python manage.py runserver >> thefile.log 2>&1 的 shell。

关于python - 为什么 subprocess.Popen 的输出与预期的不一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57216690/

相关文章:

python - 在 Lua 中什么都不做的函数

javascript - 使 CryptoJS 和 pycrypto 兼容

python - 恢复嵌套的 for 循环

python - 在 python 中查找特征值/向量的最快方法是什么?

python - 在 python/django 中异步发送邮件的正确方法是什么?

django - 如何使用多个表对 django rest 中的用户进行身份验证?

Django 上传因请求数据读取错误而失败

python - 子进程.Popen : how to terminate subprocess and receive its output?

windows - subprocess.Popen args 的编码问题

Python子进程忽略convert命令中的反斜杠