merge fix for MODLANG-15 from Traun Leyden. Thanks Traun.

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5130 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Michael Jerris 2007-05-10 18:51:47 +00:00
parent 39483cdeac
commit 3a707a316a
2 changed files with 44 additions and 26 deletions

View File

@ -9,8 +9,8 @@ SessionContainer::SessionContainer(char *nuuid)
tts_name = NULL; tts_name = NULL;
voice_name = NULL; voice_name = NULL;
if (session = switch_core_session_locate(uuid)) { if (session = switch_core_session_locate(uuid)) {
channel = switch_core_session_get_channel(session); channel = switch_core_session_get_channel(session);
} }
} }
@ -83,7 +83,10 @@ int SessionContainer::play_file(char *file, char *timer_name)
ap = &args; ap = &args;
} }
Py_BEGIN_ALLOW_THREADS
status = switch_ivr_play_file(session, NULL, file, ap); status = switch_ivr_play_file(session, NULL, file, ap);
Py_END_ALLOW_THREADS
return status == SWITCH_STATUS_SUCCESS ? 1 : 0; return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
} }
@ -114,7 +117,10 @@ int SessionContainer::speak_text(char *text)
ap = &args; ap = &args;
} }
Py_BEGIN_ALLOW_THREADS
status = switch_ivr_speak_text(session, tts_name, voice_name, codec->implementation->samples_per_second, text, ap); status = switch_ivr_speak_text(session, tts_name, voice_name, codec->implementation->samples_per_second, text, ap);
Py_END_ALLOW_THREADS
return status == SWITCH_STATUS_SUCCESS ? 1 : 0; return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
} }
@ -129,7 +135,11 @@ int SessionContainer::get_digits(char *dtmf_buf, int len, char *terminators, cha
{ {
switch_status_t status; switch_status_t status;
sanity_check(-1); sanity_check(-1);
Py_BEGIN_ALLOW_THREADS
status = switch_ivr_collect_digits_count(session, dtmf_buf,(uint32_t) len,(uint32_t) len, terminators, terminator, (uint32_t) timeout); status = switch_ivr_collect_digits_count(session, dtmf_buf,(uint32_t) len,(uint32_t) len, terminators, terminator, (uint32_t) timeout);
Py_END_ALLOW_THREADS
return status == SWITCH_STATUS_SUCCESS ? 1 : 0; return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
} }
@ -137,7 +147,11 @@ int SessionContainer::transfer(char *extension, char *dialplan, char *context)
{ {
switch_status_t status; switch_status_t status;
sanity_check(-1); sanity_check(-1);
Py_BEGIN_ALLOW_THREADS
status = switch_ivr_session_transfer(session, extension, dialplan, context); status = switch_ivr_session_transfer(session, extension, dialplan, context);
Py_END_ALLOW_THREADS
return status == SWITCH_STATUS_SUCCESS ? 1 : 0; return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
} }
@ -146,9 +160,13 @@ int SessionContainer::play_and_get_digits(int min_digits, int max_digits, int ma
{ {
switch_status_t status; switch_status_t status;
sanity_check(-1); sanity_check(-1);
Py_BEGIN_ALLOW_THREADS
status = switch_play_and_get_digits( session, (uint32_t) min_digits,(uint32_t) max_digits, status = switch_play_and_get_digits( session, (uint32_t) min_digits,(uint32_t) max_digits,
(uint32_t) max_tries, (uint32_t) timeout, (uint32_t) max_tries, (uint32_t) timeout,
terminators, audio_files, bad_input_audio_files, dtmf_buf, 128, digits_regex); terminators, audio_files, bad_input_audio_files, dtmf_buf, 128, digits_regex);
Py_END_ALLOW_THREADS
return status == SWITCH_STATUS_SUCCESS ? 1 : 0; return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
} }

View File

@ -25,6 +25,7 @@
* *
* Brian Fertig <brian.fertig@convergencetek.com> * Brian Fertig <brian.fertig@convergencetek.com>
* Johny Kadarisman <jkr888@gmail.com> * Johny Kadarisman <jkr888@gmail.com>
* Traun Leyden <tleyden@branchcut.com>
* *
* mod_python.c -- Python Module * mod_python.c -- Python Module
* *
@ -43,6 +44,7 @@
#include <switch.h> #include <switch.h>
static PyThreadState *mainThreadState = NULL;
void init_freeswitch(void); void init_freeswitch(void);
static switch_api_interface_t python_run_interface; static switch_api_interface_t python_run_interface;
@ -51,7 +53,7 @@ const char modname[] = "mod_python";
static void eval_some_python(char *uuid, char *args) static void eval_some_python(char *uuid, char *args)
{ {
PyThreadState *tstate; PyThreadState *tstate = NULL;
FILE *pythonfile = NULL; FILE *pythonfile = NULL;
char *dupargs = NULL; char *dupargs = NULL;
char *argv[128] = {0}; char *argv[128] = {0};
@ -97,16 +99,15 @@ static void eval_some_python(char *uuid, char *args)
if ((pythonfile = fopen(script_path, "r"))) { if ((pythonfile = fopen(script_path, "r"))) {
tstate = Py_NewInterpreter();
PyEval_AcquireLock();
tstate = PyThreadState_New(mainThreadState->interp);
if (!tstate) { if (!tstate) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error acquiring tstate\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error acquiring tstate\n");
goto done; goto done;
} }
PyThreadState_Swap(tstate);
init_freeswitch();
PyThreadState_Clear(tstate);
init_freeswitch();
PyRun_SimpleString("from freeswitch import *"); PyRun_SimpleString("from freeswitch import *");
if (uuid) { if (uuid) {
char code[128]; char code[128];
@ -115,7 +116,11 @@ static void eval_some_python(char *uuid, char *args)
} }
PySys_SetArgv(argc - lead, &argv[lead]); PySys_SetArgv(argc - lead, &argv[lead]);
PyRun_SimpleFile(pythonfile, script); PyRun_SimpleFile(pythonfile, script);
Py_EndInterpreter(tstate); PyThreadState_Swap(NULL);
PyThreadState_Clear(tstate);
PyThreadState_Delete(tstate);
PyEval_ReleaseLock();
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error running %s\n", script_path); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error running %s\n", script_path);
@ -134,7 +139,6 @@ static void eval_some_python(char *uuid, char *args)
static void python_function(switch_core_session_t *session, char *data) static void python_function(switch_core_session_t *session, char *data)
{ {
eval_some_python(switch_core_session_get_uuid(session), (char *)data); eval_some_python(switch_core_session_get_uuid(session), (char *)data);
} }
@ -217,9 +221,6 @@ static switch_loadable_module_interface_t python_module_interface = {
/*.directory_interface */ NULL /*.directory_interface */ NULL
}; };
//static PyThreadState *gtstate;
static PyThreadState *mainThreadState;
SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **module_interface, char *filename) SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **module_interface, char *filename)
{ {
/* connect my internal structure to the blank pointer passed to me */ /* connect my internal structure to the blank pointer passed to me */
@ -231,10 +232,9 @@ SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_mod
mainThreadState = PyThreadState_Get(); mainThreadState = PyThreadState_Get();
PyEval_ReleaseLock(); PyThreadState_Swap(NULL);
eval_some_python(NULL, "init_python.py"); PyEval_ReleaseLock();
PyThreadState_Swap(NULL);
/* indicate that the module should continue to be loaded */ /* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
@ -244,19 +244,19 @@ SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_mod
Called when the system shuts down*/ Called when the system shuts down*/
SWITCH_MOD_DECLARE(switch_status_t) switch_module_shutdown(void) SWITCH_MOD_DECLARE(switch_status_t) switch_module_shutdown(void)
{ {
PyInterpreterState *mainInterpreterState;
PyThreadState *myThreadState;
PyInterpreterState *mainInterpreterState; PyEval_AcquireLock();
PyThreadState *myThreadState; mainInterpreterState = mainThreadState->interp;
myThreadState = PyThreadState_New(mainInterpreterState);
PyThreadState_Swap(myThreadState);
PyEval_ReleaseLock();
PyEval_AcquireLock(); Py_Finalize();
mainInterpreterState = mainThreadState->interp; PyEval_ReleaseLock();
myThreadState = PyThreadState_New(mainInterpreterState); return SWITCH_STATUS_SUCCESS;
PyThreadState_Swap(myThreadState);
PyEval_ReleaseLock();
Py_Finalize();
PyEval_ReleaseLock();
return SWITCH_STATUS_SUCCESS;
} }