diff --git a/src/pyworker.c b/src/pyworker.c index 6fc68bf..75b5263 100644 --- a/src/pyworker.c +++ b/src/pyworker.c @@ -44,7 +44,7 @@ pid_t spawn(char* py_entrypoint, int wrk_id, int semid, int max_reqs) int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs) { - PyObject *entry_fun; + PyObject *entry_fun, *pystdout_flush, *pystderr_flush; int count, pipe_out[2], pipe_err[2], pipe_ctl[2], err, piper_status; struct sigaction act; sigset_t emptyset; @@ -71,6 +71,7 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs) update_python_path(); // add cwd to python path Py_Initialize(); // "start" python update_python_fd(pipe_out, pipe_err); + fetch_pyflush(&pystdout_flush, &pystderr_flush); syslog( LOG_INFO, "Worker[%d] Python started", wrk_id); @@ -128,7 +129,8 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs) syslog(LOG_DEBUG, "Worker[%d] request %d funcall [OK]", wrk_id, count); } - //TODO : flush pystdout & pystderr using python + PyObject_CallObject(pystdout_flush, NULL); + PyObject_CallObject(pystderr_flush, NULL); read(pipe_ctl[0], &buf, 1); // unblock when child ready syslog(LOG_DEBUG, "PIPER UNLOCK"); kill(pid, WPIPER_SIG); @@ -303,6 +305,40 @@ PyObject* import_entrypoint(char* py_entrypoint) return entry_fun; } +static PyObject* _fetch_pyflush(const char *fdname) +{ + PyObject *pyfd, *pyflush; + pyfd = PySys_GetObject(fdname); + if(!pyfd) + { + syslog(LOG_ALERT, "Unable to fetch sys.%s", fdname); + log_expt(LOG_ALERT); + Py_Exit(EXIT_PYERR); + } + pyflush = PyObject_GetAttrString(pyfd, "flush"); + Py_DECREF(pyfd); + if(!pyflush) + { + syslog(LOG_ALERT, "Unable to fetch sys.%s.flush", fdname); + log_expt(LOG_ALERT); + Py_Exit(EXIT_PYERR); + } + if(!PyCallable_Check(pyflush)) + { + syslog(LOG_ALERT, "sys.%s.flush is not callable !", + fdname); + Py_Exit(EXIT_PYERR); + } + + return pyflush; +} + +void fetch_pyflush(PyObject** pystdout_flush, PyObject** pystderr_flush) +{ + *pystdout_flush = _fetch_pyflush("stdout"); + *pystderr_flush = _fetch_pyflush("stderr"); +} + void update_python_path() { wchar_t *ppath, *wcwd, *wtmp; diff --git a/src/pyworker.h b/src/pyworker.h index f6fdb39..2296f53 100644 --- a/src/pyworker.h +++ b/src/pyworker.h @@ -82,6 +82,12 @@ void worker_piper_sighandler(int); */ PyObject* import_entrypoint(char* py_entrypoint); +/**@brief Fetch stdout & stderr python flush() function + * @param PyObject* pystdout_flush + * @param PyObject* pystderr_flush + */ +void fetch_pyflush(PyObject**, PyObject**); + /**@brief Add . to the embed pythonpath */ void update_python_path();