|
@@ -29,13 +29,14 @@ CScriptExecutor::CScriptExecutor(void)
|
|
|
m_bStopLogExport = FALSE;
|
|
|
m_dwThreadId = 0;
|
|
|
m_dwSubprocessId = 0;
|
|
|
+ m_bRuned = FALSE;
|
|
|
}
|
|
|
|
|
|
CScriptExecutor::~CScriptExecutor(void)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
-DWORD __stdcall CScriptExecutor::_WorkerThread(LPVOID lpParam)
|
|
|
+DWORD CScriptExecutor::_WorkerThread(LPVOID lpParam)
|
|
|
{
|
|
|
CScriptExecutor* that = (CScriptExecutor*)lpParam;
|
|
|
if ( !that )
|
|
@@ -64,7 +65,7 @@ DWORD __stdcall CScriptExecutor::_WorkerThread(LPVOID lpParam)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-DWORD __stdcall CScriptExecutor::_LogExportThread(LPVOID lpParam)
|
|
|
+DWORD CScriptExecutor::_LogExportThread(LPVOID lpParam)
|
|
|
{
|
|
|
CScriptExecutor* that = (CScriptExecutor*)lpParam;
|
|
|
if (!that)
|
|
@@ -118,6 +119,11 @@ DWORD __stdcall CScriptExecutor::_LogExportThread(LPVOID lpParam)
|
|
|
|
|
|
int CScriptExecutor::RedirectSubprocessStdout(LPSTARTUPINFO si /*=NULL*/)
|
|
|
{
|
|
|
+#ifdef _DEBUG
|
|
|
+ OutputDebugString("RedirectSubprocessStdout\n");
|
|
|
+#endif
|
|
|
+
|
|
|
+ // Python脚本中,必须使用sys.__stdout__()
|
|
|
if (m_nRunType == SUBPROCESS)
|
|
|
{
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
@@ -128,14 +134,14 @@ int CScriptExecutor::RedirectSubprocessStdout(LPSTARTUPINFO si /*=NULL*/)
|
|
|
// 创建stdout的管道;
|
|
|
if (!CreatePipe(&m_hStdOutRead, &m_hStdOutWrite, &sa, 0))
|
|
|
{
|
|
|
- printf("Error:创建stdout管道失败\n");
|
|
|
+ OutputDebugString("Error:创建stdout管道失败\n");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
// 创建stderr的管道,由于stderr一般就是stdout,直接复制句柄;
|
|
|
if (!DuplicateHandle(GetCurrentProcess(), m_hStdOutWrite, GetCurrentProcess(), &m_hStdErrorWrite, 0, TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
{
|
|
|
- printf("创建stderr管道失败\n");
|
|
|
+ OutputDebugString("创建stderr管道失败\n");
|
|
|
return -2;
|
|
|
}
|
|
|
|
|
@@ -149,13 +155,16 @@ int CScriptExecutor::RedirectSubprocessStdout(LPSTARTUPINFO si /*=NULL*/)
|
|
|
}
|
|
|
|
|
|
// 异常类型;
|
|
|
- printf("Error:异常类型\n");
|
|
|
+ OutputDebugString("Error:异常类型\n");
|
|
|
|
|
|
return -5;
|
|
|
}
|
|
|
|
|
|
int CScriptExecutor::RedirectProcessStdout()
|
|
|
{
|
|
|
+#ifdef _DEBUG
|
|
|
+ OutputDebugString("RedirectProcessStdout\n");
|
|
|
+#endif
|
|
|
if (m_nRunType == EMBEDDED)
|
|
|
{
|
|
|
// 创建stdout的管道;
|
|
@@ -356,6 +365,7 @@ int CScriptExecutor::RunEmbeddedScript()
|
|
|
return -4;
|
|
|
}
|
|
|
|
|
|
+ m_bRuned = TRUE;
|
|
|
// 执行main函数;
|
|
|
PyObject* pResult = PyObject_CallObject(pFunction, NULL);
|
|
|
if (!pResult)
|
|
@@ -363,8 +373,10 @@ int CScriptExecutor::RunEmbeddedScript()
|
|
|
printf("Error:执行函数失败\n");
|
|
|
CatchPythonException();
|
|
|
Py_Finalize();
|
|
|
+
|
|
|
return -5;
|
|
|
}
|
|
|
+
|
|
|
Py_DECREF(pResult);
|
|
|
|
|
|
Py_Finalize();
|
|
@@ -386,11 +398,12 @@ int CScriptExecutor::RunScripProcess()
|
|
|
m_si.cb = sizeof(m_si);
|
|
|
GetStartupInfo(&m_si);
|
|
|
|
|
|
+ // 强制stdion, stdout和stderr完全无缓冲:python -u
|
|
|
TCHAR szCommandLine[MAX_PATH] = { 0 };
|
|
|
if (_tcslen(m_szExtraSentence))
|
|
|
- _stprintf_s(szCommandLine, _T("python -W ignore %s %s"), m_szScriptPath, m_szExtraSentence);
|
|
|
+ _stprintf_s(szCommandLine, _T("python -W ignore -u %s %s"), m_szScriptPath, m_szExtraSentence);
|
|
|
else
|
|
|
- _stprintf_s(szCommandLine, _T("python -W ignore %s"), m_szScriptPath);
|
|
|
+ _stprintf_s(szCommandLine, _T("python -W ignore -u %s"), m_szScriptPath);
|
|
|
|
|
|
// 重定向输出;
|
|
|
RedirectSubprocessStdout(&m_si);
|
|
@@ -415,9 +428,12 @@ int CScriptExecutor::RunScripProcess()
|
|
|
return -3;
|
|
|
}
|
|
|
|
|
|
+ m_bRuned = TRUE;
|
|
|
m_dwSubprocessId = m_pi.dwProcessId;
|
|
|
// 等待进程完成退出.
|
|
|
WaitForSingleObject(m_pi.hProcess, INFINITE);
|
|
|
+
|
|
|
+ Sleep(5000);
|
|
|
// 结束日志线程;
|
|
|
m_bStopLogExport = TRUE;
|
|
|
|
|
@@ -448,6 +464,7 @@ bool CScriptExecutor::StartWorkThread()
|
|
|
|
|
|
bool CScriptExecutor::StartLogThread()
|
|
|
{
|
|
|
+ printf("StartLogThread\n");
|
|
|
// 创建线程事件:手动控制,无信号状态;
|
|
|
m_hLogEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
if (!m_hLogEvent)
|
|
@@ -485,8 +502,6 @@ void CScriptExecutor::EndWorkThread()
|
|
|
CloseHandle(m_hWorkThread);
|
|
|
m_hWorkThread = NULL;
|
|
|
}
|
|
|
-
|
|
|
- m_dwThreadId = m_dwSubprocessId = 0;
|
|
|
}
|
|
|
|
|
|
void CScriptExecutor::EndLogThread()
|
|
@@ -542,8 +557,6 @@ BOOL CScriptExecutor::EndSubprocess()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- m_dwSubprocessId = 0;
|
|
|
-
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
@@ -636,7 +649,9 @@ void CScriptExecutor::InitScript(std::string strScript, std::string strLogPath,
|
|
|
}
|
|
|
|
|
|
// 赋值参数;
|
|
|
+ m_bRuned = FALSE;
|
|
|
m_nRunType = nRunType;
|
|
|
+ m_dwThreadId = m_dwSubprocessId = 0;
|
|
|
_tcscpy_s(m_szScriptPath, strScript.c_str());
|
|
|
_tcscpy_s(m_szLogPath, strLogPath.c_str());
|
|
|
_tcscpy_s(m_szExtraSentence, strScriptCmd.c_str());
|
|
@@ -675,6 +690,14 @@ void CScriptExecutor::StopScript()
|
|
|
|
|
|
bool CScriptExecutor::IsScriptOver()
|
|
|
{
|
|
|
+ int i = 10;
|
|
|
+ while (!m_bRuned)
|
|
|
+ {
|
|
|
+ Sleep(300);
|
|
|
+ if ( --i == 0 )
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
if (m_nRunType == EMBEDDED)
|
|
|
{
|
|
|
if ( m_hWorkThread == NULL )
|