提交 21f90068 authored 作者: Josh Bleecher Snyder's avatar Josh Bleecher Snyder

Make sure the compilelock gets released

上级 a1030ea7
...@@ -11,56 +11,57 @@ if os.path.exists(os.path.join(config.compiledir,'cutils_ext.so')): ...@@ -11,56 +11,57 @@ if os.path.exists(os.path.join(config.compiledir,'cutils_ext.so')):
# directory. This is important to prevent multiple processes from trying to # directory. This is important to prevent multiple processes from trying to
# compile the cutils_ext module simultaneously. # compile the cutils_ext module simultaneously.
get_lock() get_lock()
try: try:
from cutils_ext.cutils_ext import * try:
except ImportError: from cutils_ext.cutils_ext import *
import cmodule except ImportError:
import cmodule
code = """ code = """
#include <Python.h> #include <Python.h>
extern "C"{ extern "C"{
static PyObject * static PyObject *
run_cthunk(PyObject *self, PyObject *args) run_cthunk(PyObject *self, PyObject *args)
{ {
PyObject *py_cthunk = NULL; PyObject *py_cthunk = NULL;
if(!PyArg_ParseTuple(args,"O",&py_cthunk)) if(!PyArg_ParseTuple(args,"O",&py_cthunk))
return NULL; return NULL;
if (!PyCObject_Check(py_cthunk)) { if (!PyCObject_Check(py_cthunk)) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"Argument to run_cthunk must be a PyCObject."); "Argument to run_cthunk must be a PyCObject.");
return NULL; return NULL;
} }
void * ptr_addr = PyCObject_AsVoidPtr(py_cthunk); void * ptr_addr = PyCObject_AsVoidPtr(py_cthunk);
int (*fn)(void*) = (int (*)(void*))(ptr_addr); int (*fn)(void*) = (int (*)(void*))(ptr_addr);
void* it = PyCObject_GetDesc(py_cthunk); void* it = PyCObject_GetDesc(py_cthunk);
int failure = fn(it); int failure = fn(it);
return Py_BuildValue("i", failure); return Py_BuildValue("i", failure);
} }
static PyMethodDef CutilsExtMethods[] = { static PyMethodDef CutilsExtMethods[] = {
{"run_cthunk", run_cthunk, METH_VARARGS|METH_KEYWORDS, {"run_cthunk", run_cthunk, METH_VARARGS|METH_KEYWORDS,
"Run a theano cthunk."}, "Run a theano cthunk."},
{NULL, NULL, 0, NULL} /* Sentinel */ {NULL, NULL, 0, NULL} /* Sentinel */
}; };
PyMODINIT_FUNC PyMODINIT_FUNC
initcutils_ext(void) initcutils_ext(void)
{ {
(void) Py_InitModule("cutils_ext", CutilsExtMethods); (void) Py_InitModule("cutils_ext", CutilsExtMethods);
} }
} }
""" """
loc = os.path.join(config.compiledir, 'cutils_ext') loc = os.path.join(config.compiledir, 'cutils_ext')
if not os.path.exists(loc): if not os.path.exists(loc):
os.mkdir(loc) os.mkdir(loc)
cmodule.gcc_module_compile_str('cutils_ext', code, location=loc) cmodule.gcc_module_compile_str('cutils_ext', code, location=loc)
from cutils_ext.cutils_ext import * from cutils_ext.cutils_ext import *
# Release lock on compilation directory. finally:
release_lock() # Release lock on compilation directory.
release_lock()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论