2929#include < Modules/Python/InterfaceWithPython.h>
3030#include < Core/Datatypes/String.h>
3131#include < Core/Datatypes/DenseMatrix.h>
32+ #include < Core/Python/PythonInterpreter.h>
3233#include < boost/thread.hpp>
3334
3435using namespace SCIRun ::Modules::Python;
3536using namespace SCIRun ::Core::Datatypes;
3637using namespace SCIRun ::Dataflow::Networks;
38+ using namespace SCIRun ::Core;
39+ using namespace SCIRun ::Core::Thread;
3740using namespace SCIRun ::Core::Algorithms;
3841using namespace SCIRun ::Core::Algorithms::Python;
3942
@@ -42,6 +45,7 @@ using namespace SCIRun::Core::Algorithms::Python;
4245ALGORITHM_PARAMETER_DEF (Python, PythonCode);
4346
4447const ModuleLookupInfo InterfaceWithPython::staticInfo_ (" InterfaceWithPython" , " Python" , " SCIRun" );
48+ Mutex InterfaceWithPython::lock_ (" InterfaceWithPython" );
4549
4650InterfaceWithPython::InterfaceWithPython () : Module(staticInfo_)
4751{
@@ -61,48 +65,8 @@ void InterfaceWithPython::setStateDefaults()
6165
6266void InterfaceWithPython::execute ()
6367{
64- /*
68+ Guard g (lock_. get ());
6569 auto state = get_state ();
66- int tries = 0;
67- const int maxTries = state->getValue(Parameters::NumberOfRetries).toInt();
68- const int waitTime = state->getValue(Parameters::PollingIntervalMilliseconds).toInt();
69- auto valueOption = state->getTransientValue(Parameters::PythonObject);
70-
71- while (tries < maxTries && !valueOption)
72- {
73- std::ostringstream ostr;
74- ostr << "PythonObjectForwarder looking up value attempt #" << (tries+1) << "/" << maxTries;
75- remark(ostr.str());
76-
77- valueOption = state->getTransientValue(Parameters::PythonObject);
78-
79- tries++;
80- boost::this_thread::sleep(boost::posix_time::milliseconds(waitTime));
81- }
82-
83- if (valueOption)
84- {
85- auto var = transient_value_cast<Variable>(valueOption);
86- if (var.name().name() == "string")
87- {
88- auto valueStr = var.toString();
89- if (!valueStr.empty())
90- sendOutput(PythonString, boost::make_shared<String>(valueStr));
91- else
92- sendOutput(PythonString, boost::make_shared<String>("Empty string or non-string received"));
93- }
94- else if (var.name().name() == "dense matrix")
95- {
96- auto dense = boost::dynamic_pointer_cast<DenseMatrix>(var.getDatatype());
97- if (dense)
98- sendOutput(PythonMatrix, dense);
99- }
100- else if (var.name().name() == "sparse matrix")
101- {
102- auto sparse = boost::dynamic_pointer_cast<SparseRowMatrix>(var.getDatatype());
103- if (sparse)
104- sendOutput(PythonMatrix, sparse);
105- }
106- }
107- */
70+ auto code = state->getValue (Parameters::PythonCode).toString ();
71+ PythonInterpreter::Instance ().run_string (code);
10872}
0 commit comments