Python initialization moved from master to worker application.

This commit is contained in:
Max Romanov
2017-07-07 16:01:34 +03:00
parent 74cda90e31
commit 5529e9f0d1

View File

@@ -66,7 +66,7 @@ static nxt_int_t nxt_python_prepare_msg(nxt_task_t *task,
static nxt_int_t nxt_python_run(nxt_task_t *task, static nxt_int_t nxt_python_run(nxt_task_t *task,
nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *msg); nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *msg);
static PyObject *nxt_python_create_environ(nxt_thread_t *thr); static PyObject *nxt_python_create_environ(nxt_task_t *task);
static PyObject *nxt_python_get_environ(nxt_task_t *task, static PyObject *nxt_python_get_environ(nxt_task_t *task,
nxt_app_rmsg_t *rmsg); nxt_app_rmsg_t *rmsg);
@@ -181,15 +181,13 @@ static nxt_python_run_ctx_t *nxt_python_run_ctx;
nxt_int_t nxt_int_t
nxt_python_wsgi_init(nxt_thread_t *thr, nxt_runtime_t *rt) nxt_python_wsgi_init(nxt_thread_t *thr, nxt_runtime_t *rt)
{ {
char **argv; char **argv;
u_char *p, *dir; char *p;
PyObject *obj, *pypath;
argv = nxt_process_argv; argv = nxt_process_argv;
while (*argv != NULL) { while (*argv != NULL) {
p = (u_char *) *argv++; p = *argv++;
if (nxt_strcmp(p, "--py-module") == 0) { if (nxt_strcmp(p, "--py-module") == 0) {
if (*argv == NULL) { if (*argv == NULL) {
@@ -211,28 +209,43 @@ nxt_python_wsgi_init(nxt_thread_t *thr, nxt_runtime_t *rt)
return NXT_OK; return NXT_OK;
} }
nxt_app = &nxt_python_module;
return NXT_OK;
}
static nxt_int_t
nxt_python_init(nxt_task_t *task)
{
char **argv;
char *p, *dir;
PyObject *obj, *pypath, *module;
Py_InitializeEx(0); Py_InitializeEx(0);
obj = NULL; obj = NULL;
module = NULL;
argv = nxt_process_argv; argv = nxt_process_argv;
while (*argv != NULL) { while (*argv != NULL) {
p = (u_char *) *argv++; p = *argv++;
if (nxt_strcmp(p, "--py-path") == 0) { if (nxt_strcmp(p, "--py-path") == 0) {
if (*argv == NULL) { if (*argv == NULL) {
nxt_log_emerg(thr->log, "no argument for option \"--py-path\""); nxt_log_emerg(task->log, "no argument for option \"--py-path\"");
goto fail; goto fail;
} }
dir = (u_char *) *argv++; dir = *argv++;
nxt_log_error(NXT_LOG_INFO, thr->log, "python path \"%s\"", dir); nxt_log_error(NXT_LOG_INFO, task->log, "python path \"%s\"", dir);
obj = PyString_FromString((char *) dir); obj = PyString_FromString((char *) dir);
if (nxt_slow_path(obj == NULL)) { if (nxt_slow_path(obj == NULL)) {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed create string object \"%s\"", dir); "Python failed create string object \"%s\"", dir);
goto fail; goto fail;
} }
@@ -240,13 +253,13 @@ nxt_python_wsgi_init(nxt_thread_t *thr, nxt_runtime_t *rt)
pypath = PySys_GetObject((char *) "path"); pypath = PySys_GetObject((char *) "path");
if (nxt_slow_path(pypath == NULL)) { if (nxt_slow_path(pypath == NULL)) {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to get \"sys.path\" list"); "Python failed to get \"sys.path\" list");
goto fail; goto fail;
} }
if (nxt_slow_path(PyList_Insert(pypath, 0, obj) != 0)) { if (nxt_slow_path(PyList_Insert(pypath, 0, obj) != 0)) {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to insert \"%s\" into \"sys.path\"", dir); "Python failed to insert \"%s\" into \"sys.path\"", dir);
goto fail; goto fail;
} }
@@ -261,14 +274,14 @@ nxt_python_wsgi_init(nxt_thread_t *thr, nxt_runtime_t *rt)
obj = PyCFunction_New(nxt_py_start_resp_method, NULL); obj = PyCFunction_New(nxt_py_start_resp_method, NULL);
if (nxt_slow_path(obj == NULL)) { if (nxt_slow_path(obj == NULL)) {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to initialize the \"start_response\" function"); "Python failed to initialize the \"start_response\" function");
goto fail; goto fail;
} }
nxt_py_start_resp_obj = obj; nxt_py_start_resp_obj = obj;
obj = nxt_python_create_environ(thr); obj = nxt_python_create_environ(task);
if (obj == NULL) { if (obj == NULL) {
goto fail; goto fail;
@@ -276,83 +289,21 @@ nxt_python_wsgi_init(nxt_thread_t *thr, nxt_runtime_t *rt)
nxt_py_environ_ptyp = obj; nxt_py_environ_ptyp = obj;
obj = Py_BuildValue("[s]", "nginext"); obj = Py_BuildValue("[s]", "nginext");
if (obj == NULL) { if (obj == NULL) {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to create the \"sys.argv\" list"); "Python failed to create the \"sys.argv\" list");
goto fail; goto fail;
} }
if (PySys_SetObject((char *) "argv", obj) != 0) { if (PySys_SetObject((char *) "argv", obj) != 0) {
nxt_log_alert(thr->log, "Python failed to set the \"sys.argv\" list"); nxt_log_alert(task->log, "Python failed to set the \"sys.argv\" list");
goto fail; goto fail;
} }
Py_DECREF(obj); Py_DECREF(obj);
nxt_app = &nxt_python_module; // PyOS_AfterFork();
return NXT_OK;
fail:
Py_XDECREF(obj);
Py_XDECREF(nxt_py_start_resp_obj);
Py_Finalize();
return NXT_ERROR;
}
static nxt_int_t
nxt_python_init(nxt_task_t *task)
{
PyObject *module, *obj;
#if 0
FILE *fp;
PyObject *co;
struct _node *node;
chdir((char *) dir);
fp = fopen((char *) script, "r");
if (fp == NULL) {
nxt_log_debug(thr->log, "fopen failed");
return NXT_ERROR;
}
Py_SetProgramName((char *) "python mysite/wsgi.py");
Py_InitializeEx(0);
node = PyParser_SimpleParseFile(fp, (char *) script, Py_file_input);
fclose(fp);
if (node == NULL) {
nxt_log_debug(thr->log, "BAD node");
return NXT_ERROR;
}
co = (PyObject *) PyNode_Compile(node, (char *) script);
PyNode_Free(node);
if (co == NULL) {
nxt_log_debug(thr->log, "BAD co");
return NXT_ERROR;
}
pModule = PyImport_ExecCodeModuleEx((char *) "_wsgi_nginext", co,
(char *) script);
Py_XDECREF(co);
#endif
PyOS_AfterFork();
module = PyImport_ImportModule(nxt_py_module); module = PyImport_ImportModule(nxt_py_module);
@@ -387,6 +338,7 @@ nxt_python_init(nxt_task_t *task)
fail: fail:
Py_DECREF(obj);
Py_DECREF(module); Py_DECREF(module);
return NXT_ERROR; return NXT_ERROR;
@@ -586,14 +538,14 @@ fail:
static PyObject * static PyObject *
nxt_python_create_environ(nxt_thread_t *thr) nxt_python_create_environ(nxt_task_t *task)
{ {
PyObject *obj, *err, *environ; PyObject *obj, *err, *environ;
environ = PyDict_New(); environ = PyDict_New();
if (nxt_slow_path(environ == NULL)) { if (nxt_slow_path(environ == NULL)) {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to create the \"environ\" dictionary"); "Python failed to create the \"environ\" dictionary");
return NULL; return NULL;
} }
@@ -601,14 +553,14 @@ nxt_python_create_environ(nxt_thread_t *thr)
obj = Py_BuildValue("(ii)", 1, 0); obj = Py_BuildValue("(ii)", 1, 0);
if (nxt_slow_path(obj == NULL)) { if (nxt_slow_path(obj == NULL)) {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to build the \"wsgi.version\" environ value"); "Python failed to build the \"wsgi.version\" environ value");
goto fail; goto fail;
} }
if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.version", obj) != 0)) if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.version", obj) != 0))
{ {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to set the \"wsgi.version\" environ value"); "Python failed to set the \"wsgi.version\" environ value");
goto fail; goto fail;
} }
@@ -621,7 +573,7 @@ nxt_python_create_environ(nxt_thread_t *thr)
Py_False) Py_False)
!= 0)) != 0))
{ {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to set the \"wsgi.multithread\" environ value"); "Python failed to set the \"wsgi.multithread\" environ value");
goto fail; goto fail;
} }
@@ -630,7 +582,7 @@ nxt_python_create_environ(nxt_thread_t *thr)
Py_True) Py_True)
!= 0)) != 0))
{ {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to set the \"wsgi.multiprocess\" environ value"); "Python failed to set the \"wsgi.multiprocess\" environ value");
goto fail; goto fail;
} }
@@ -639,7 +591,7 @@ nxt_python_create_environ(nxt_thread_t *thr)
Py_False) Py_False)
!= 0)) != 0))
{ {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to set the \"wsgi.run_once\" environ value"); "Python failed to set the \"wsgi.run_once\" environ value");
goto fail; goto fail;
} }
@@ -648,7 +600,7 @@ nxt_python_create_environ(nxt_thread_t *thr)
obj = PyString_FromString("http"); obj = PyString_FromString("http");
if (nxt_slow_path(obj == NULL)) { if (nxt_slow_path(obj == NULL)) {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to create the \"wsgi.url_scheme\" environ value"); "Python failed to create the \"wsgi.url_scheme\" environ value");
goto fail; goto fail;
} }
@@ -656,7 +608,7 @@ nxt_python_create_environ(nxt_thread_t *thr)
if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.url_scheme", obj) if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.url_scheme", obj)
!= 0)) != 0))
{ {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to set the \"wsgi.url_scheme\" environ value"); "Python failed to set the \"wsgi.url_scheme\" environ value");
goto fail; goto fail;
} }
@@ -666,7 +618,7 @@ nxt_python_create_environ(nxt_thread_t *thr)
if (nxt_slow_path(PyType_Ready(&nxt_py_input_type) != 0)) { if (nxt_slow_path(PyType_Ready(&nxt_py_input_type) != 0)) {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to initialize the \"wsgi.input\" type object"); "Python failed to initialize the \"wsgi.input\" type object");
goto fail; goto fail;
} }
@@ -674,13 +626,13 @@ nxt_python_create_environ(nxt_thread_t *thr)
obj = (PyObject *) PyObject_New(nxt_py_input_t, &nxt_py_input_type); obj = (PyObject *) PyObject_New(nxt_py_input_t, &nxt_py_input_type);
if (nxt_slow_path(obj == NULL)) { if (nxt_slow_path(obj == NULL)) {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to create the \"wsgi.input\" object"); "Python failed to create the \"wsgi.input\" object");
goto fail; goto fail;
} }
if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.input", obj) != 0)) { if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.input", obj) != 0)) {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to set the \"wsgi.input\" environ value"); "Python failed to set the \"wsgi.input\" environ value");
goto fail; goto fail;
} }
@@ -692,13 +644,13 @@ nxt_python_create_environ(nxt_thread_t *thr)
err = PySys_GetObject((char *) "stderr"); err = PySys_GetObject((char *) "stderr");
if (nxt_slow_path(err == NULL)) { if (nxt_slow_path(err == NULL)) {
nxt_log_alert(thr->log, "Python failed to get \"sys.stderr\" object"); nxt_log_alert(task->log, "Python failed to get \"sys.stderr\" object");
goto fail; goto fail;
} }
if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.error", err) != 0)) if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.error", err) != 0))
{ {
nxt_log_alert(thr->log, nxt_log_alert(task->log,
"Python failed to set the \"wsgi.error\" environ value"); "Python failed to set the \"wsgi.error\" environ value");
goto fail; goto fail;
} }