NB.我已经看到了multiprocessing.Process的日志输出 - 遗憾的是,它没有回答这个问题.
我正在通过多处理创建一个子进程(在Windows上).我希望将所有子进程的stdout和stderr输出重定向到日志文件,而不是出现在控制台上.我看到的唯一建议是子进程将sys.stdout设置为文件.但是,由于Windows上的stdout重定向行为,这不能有效地重定向所有stdout输出.
要说明此问题,请使用以下代码构建Windows DLL
#include <iostream>
extern "C"
{
__declspec(dllexport) void writeToStdOut()
{
std::cout << "Writing to STDOUT from test DLL" << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
然后创建并运行如下所示的python脚本,它导入此DLL并调用该函数:
from ctypes import *
import sys
print
print "Writing to STDOUT from python, before redirect"
print
sys.stdout = open("stdout_redirect_log.txt", "w")
print "Writing to STDOUT from python, after redirect"
testdll = CDLL("Release/stdout_test.dll")
testdll.writeToStdOut()
Run Code Online (Sandbox Code Playgroud)
为了看到与我相同的行为,可能需要针对与Python使用的不同的C运行时构建DLL.在我的例子中,python是使用Visual Studio 2010构建的,但我的DLL是使用VS 2005构建的.
我看到的行为是控制台显示:
> stdout_test.py
Writing to STDOUT from python, before redirect
Writing to STDOUT …Run Code Online (Sandbox Code Playgroud)