Skip to content
Snippets Groups Projects
Unverified Commit bed0955f authored by Pete Peterson's avatar Pete Peterson Committed by GitHub
Browse files

Merge pull request #23267 from mantidproject/23266_add_GIL_release

Added GIL release to findRuns python calls
parents c77ba558 c4008b8a
No related branches found
No related tags found
No related merge requests found
#ifndef MANTID_PYTHONINTERFACE_RELEASEGLOBALINTERPRETERLOCK_H_
#define MANTID_PYTHONINTERFACE_RELEASEGLOBALINTERPRETERLOCK_H_
#include "MantidPythonInterface/kernel/DllConfig.h"
#include <boost/python/detail/wrap_python.hpp>
namespace Mantid {
namespace PythonInterface {
namespace Environment {
/**
* Defines a structure for releaseing the Python GIL
* using the RAII pattern. This releases the Python GIL
* for the duration of the current scope.
*/
class PYTHON_KERNEL_DLL ReleaseGlobalInterpreterLock {
public:
/// Default constructor
ReleaseGlobalInterpreterLock();
/// Destructor
~ReleaseGlobalInterpreterLock();
private:
// Stores the current python trace used to track where in
// a python script you are.
Py_tracefunc m_tracefunc;
PyObject *m_tracearg;
/// Saved thread state
PyThreadState *m_saved;
};
} // namespace Environment
} // namespace PythonInterface
} // namespace Mantid
#endif /* MANTID_PYTHONINTERFACE_RELEASEGLOBALINTERPRETERLock_H_ */
#include "MantidAPI/FileFinder.h"
#include "MantidKernel/WarningSuppressions.h"
#include "MantidPythonInterface/kernel/Environment/ReleaseGlobalInterpreterLock.h"
#include <boost/python/class.hpp>
#include <boost/python/overloads.hpp>
#include <boost/python/reference_existing_object.hpp>
......@@ -20,6 +21,23 @@ GNU_DIAG_ON("conversion")
GNU_DIAG_ON("unused-local-typedef")
} // namespace
/**
* Runs FileFinder.findRuns after releasing the python GIL.
* @param self :: A reference to the calling object
* @param hinstr :: A string containing the run number and possibly instrument
* to search for
*/
std::vector<std::string> runFinderProxy(FileFinderImpl &self,
std::string hinstr) {
// Before calling the function we need to release the GIL,
// drop the Python threadstate and reset anything installed
// via PyEval_SetTrace while we execute the C++ code -
// ReleaseGlobalInterpreter does this for us
Mantid::PythonInterface::Environment::ReleaseGlobalInterpreterLock
releaseGlobalInterpreterLock;
return self.findRuns(hinstr);
}
void export_FileFinder() {
class_<FileFinderImpl, boost::noncopyable>("FileFinderImpl", no_init)
.def("getFullPath", &FileFinderImpl::getFullPath,
......@@ -28,7 +46,7 @@ void export_FileFinder() {
"Return a full path to the given file if it can be found within "
"datasearch.directories paths. Directories can be ignored with "
"ignoreDirs=True. An empty string is returned otherwise."))
.def("findRuns", &FileFinderImpl::findRuns, (arg("self"), arg("hintstr")),
.def("findRuns", &runFinderProxy, (arg("self"), arg("hintstr")),
"Find a list of files file given a hint. "
"The hint can be a comma separated list of run numbers and can also "
"include ranges of runs, e.g. 123-135 or equivalently 123-35"
......
......@@ -89,6 +89,7 @@ set ( SRC_FILES
src/Registry/TypeRegistry.cpp
src/Environment/ErrorHandling.cpp
src/Environment/GlobalInterpreterLock.cpp
src/Environment/ReleaseGlobalInterpreterLock.cpp
src/Environment/WrapperHelpers.cpp
)
......
#include "MantidPythonInterface/kernel/Environment/ReleaseGlobalInterpreterLock.h"
namespace Mantid {
namespace PythonInterface {
namespace Environment {
/**
* Ensures this thread releases the Python GIL also save trace information
* to be restored upon destruction.
*/
ReleaseGlobalInterpreterLock::ReleaseGlobalInterpreterLock()
: m_tracefunc(nullptr), m_tracearg(nullptr), m_saved(nullptr) {
PyThreadState *curThreadState = PyThreadState_GET();
m_tracefunc = curThreadState->c_tracefunc;
m_tracearg = curThreadState->c_traceobj;
Py_XINCREF(m_tracearg);
PyEval_SetTrace(nullptr, nullptr);
m_saved = PyEval_SaveThread();
}
/**
* Restores the Python GIL to the thread when the object falls out of scope.
*/
ReleaseGlobalInterpreterLock::~ReleaseGlobalInterpreterLock() {
PyEval_RestoreThread(m_saved);
PyEval_SetTrace(m_tracefunc, m_tracearg);
Py_XDECREF(m_tracearg);
}
} // namespace Environment
} // namespace PythonInterface
} // namespace Mantid
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment