Skip to content

Commit a7beaa1

Browse files
committed
Super basic impl
1 parent 2045347 commit a7beaa1

File tree

3 files changed

+11
-43
lines changed

3 files changed

+11
-43
lines changed

src/Modules/Python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ TARGET_LINK_LIBRARIES(Modules_Python
4747
Core_Datatypes
4848
Algorithms_Base
4949
Core_Python
50+
Core_Thread
5051
)
5152

5253
IF(BUILD_SHARED_LIBS)

src/Modules/Python/InterfaceWithPython.cc

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@
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

3435
using namespace SCIRun::Modules::Python;
3536
using namespace SCIRun::Core::Datatypes;
3637
using namespace SCIRun::Dataflow::Networks;
38+
using namespace SCIRun::Core;
39+
using namespace SCIRun::Core::Thread;
3740
using namespace SCIRun::Core::Algorithms;
3841
using namespace SCIRun::Core::Algorithms::Python;
3942

@@ -42,6 +45,7 @@ using namespace SCIRun::Core::Algorithms::Python;
4245
ALGORITHM_PARAMETER_DEF(Python, PythonCode);
4346

4447
const ModuleLookupInfo InterfaceWithPython::staticInfo_("InterfaceWithPython", "Python", "SCIRun");
48+
Mutex InterfaceWithPython::lock_("InterfaceWithPython");
4549

4650
InterfaceWithPython::InterfaceWithPython() : Module(staticInfo_)
4751
{
@@ -61,48 +65,8 @@ void InterfaceWithPython::setStateDefaults()
6165

6266
void 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
}

src/Modules/Python/InterfaceWithPython.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define MODULES_PYTHON_INTERFACEWITHPYTHON_H
3131

3232
#include <Dataflow/Network/Module.h>
33+
#include <Core/Thread/Mutex.h>
3334
#include <Modules/Python/share.h>
3435

3536
namespace SCIRun
@@ -68,6 +69,8 @@ namespace SCIRun
6869
OUTPUT_PORT(2, PythonString, String);
6970

7071
static const Dataflow::Networks::ModuleLookupInfo staticInfo_;
72+
private:
73+
static Core::Thread::Mutex lock_;
7174
};
7275

7376
}

0 commit comments

Comments
 (0)