Skip to content

Commit

Permalink
release GIL before potentially heavy computations
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshikikanemoto committed Jan 10, 2024
1 parent c19b7c9 commit 41af665
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
15 changes: 13 additions & 2 deletions python/bindings/openravepy_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2001,6 +2001,7 @@ void PyEnvironmentBase::Add(PyInterfaceBasePtr pinterface, py::object oAddMode,
addMode = py::extract<InterfaceAddMode>(oAddMode);
}
}
PythonThreadSaver threadsaver;
_penv->Add(pinterface->GetInterfaceBase(), addMode, cmdargs);
}

Expand Down Expand Up @@ -2463,14 +2464,24 @@ object PyEnvironmentBase::plot3(object opoints,float pointsize,object ocolors, i
pair<size_t,size_t> sizes = _getGraphPointsColors(opoints,ocolors,vpoints,vcolors);
bool bhasalpha = vcolors.size() == 4*sizes.second;
if( sizes.first == sizes.second ) {
return toPyGraphHandle(_penv->plot3(vpoints.data(),sizes.first,sizeof(float)*3,pointsize,vcolors.data(),drawstyle,bhasalpha));
GraphHandlePtr phandle;
{
PythonThreadSaver saver;
phandle = _penv->plot3(vpoints.data(),sizes.first,sizeof(float)*3,pointsize,vcolors.data(),drawstyle,bhasalpha);
}
return toPyGraphHandle(phandle);
}
BOOST_ASSERT(vcolors.size()<=4);
RaveVector<float> vcolor;
for(int i = 0; i < (int)vcolors.size(); ++i) {
vcolor[i] = vcolors[i];
}
return toPyGraphHandle(_penv->plot3(vpoints.data(),sizes.first,sizeof(float)*3,pointsize,vcolor,drawstyle));
GraphHandlePtr phandle;
{
PythonThreadSaver saver;
phandle = _penv->plot3(vpoints.data(),sizes.first,sizeof(float)*3,pointsize,vcolor,drawstyle);
}
return toPyGraphHandle(phandle);
}

object PyEnvironmentBase::drawlinestrip(object opoints,float linewidth,object ocolors, int drawstyle)
Expand Down
35 changes: 31 additions & 4 deletions python/bindings/openravepy_kinbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,11 @@ object PyGeometryInfo::SerializeJSON(dReal fUnitScale, object options)
{
rapidjson::Document doc;
KinBody::GeometryInfoPtr pgeominfo = GetGeometryInfo();
pgeominfo->SerializeJSON(doc, doc.GetAllocator(), fUnitScale, pyGetIntFromPy(options, 0));
const int intOption = pyGetIntFromPy(options, 0);
{
openravepy::PythonThreadSaver threadsaver;
pgeominfo->SerializeJSON(doc, doc.GetAllocator(), fUnitScale, intOption);
}
return toPyObject(doc);
}

Expand All @@ -375,7 +379,11 @@ void PyGeometryInfo::DeserializeJSON(object obj, dReal fUnitScale, object option
rapidjson::Document doc;
toRapidJSONValue(obj, doc, doc.GetAllocator());
KinBody::GeometryInfoPtr pgeominfo = GetGeometryInfo();
pgeominfo->DeserializeJSON(doc, fUnitScale, pyGetIntFromPy(options, 0));
const int intOption = pyGetIntFromPy(options, 0);
{
openravepy::PythonThreadSaver threadsaver;
pgeominfo->DeserializeJSON(doc, fUnitScale, intOption);
}
Init(*pgeominfo);
}

Expand Down Expand Up @@ -608,7 +616,11 @@ py::object PyLinkInfo::SerializeJSON(dReal fUnitScale, object options)
{
rapidjson::Document doc;
KinBody::LinkInfoPtr pInfo = GetLinkInfo();
pInfo->SerializeJSON(doc, doc.GetAllocator(), fUnitScale, pyGetIntFromPy(options, 0));
const int intOption = pyGetIntFromPy(options, 0);
{
openravepy::PythonThreadSaver threadsaver;
pInfo->SerializeJSON(doc, doc.GetAllocator(), fUnitScale, intOption);
}
return toPyObject(doc);
}

Expand All @@ -617,7 +629,11 @@ void PyLinkInfo::DeserializeJSON(object obj, dReal fUnitScale, py::object option
rapidjson::Document doc;
toRapidJSONValue(obj, doc, doc.GetAllocator());
KinBody::LinkInfoPtr pInfo = GetLinkInfo();
pInfo->DeserializeJSON(doc, fUnitScale, pyGetIntFromPy(options, 0));
const int intOption = pyGetIntFromPy(options, 0);
{
openravepy::PythonThreadSaver threadsaver;
pInfo->DeserializeJSON(doc, fUnitScale, intOption);
}
_Update(*pInfo);
}

Expand Down Expand Up @@ -1775,6 +1791,7 @@ void PyLink::InitGeometries(object ogeometryinfos)
}
geometries[i] = pygeom->GetGeometryInfo();
}
openravepy::PythonThreadSaver threadsaver;
return _plink->InitGeometries(geometries);
}

Expand Down Expand Up @@ -1825,6 +1842,7 @@ void PyLink::SetGroupGeometries(const std::string& name, object ogeometryinfos)
}
geometries[i] = pygeom->GetGeometryInfo();
}
openravepy::PythonThreadSaver threadsaver;
_plink->SetGroupGeometries(name, geometries);
}

Expand Down Expand Up @@ -2692,6 +2710,7 @@ bool PyKinBody::InitFromKinBodyInfo(const object pyKinBodyInfo)
KinBody::KinBodyInfoPtr pKinBodyInfo;
pKinBodyInfo = ExtractKinBodyInfo(pyKinBodyInfo);
if(!!pKinBodyInfo) {
openravepy::PythonThreadSaver threadsaver;
return _pbody->InitFromKinBodyInfo(*pKinBodyInfo);
}
return false;
Expand Down Expand Up @@ -2720,6 +2739,7 @@ bool PyKinBody::InitFromBoxes(const boost::multi_array<dReal,2>& vboxes, bool bD
vaabbs[i].pos = Vector(vboxes[i][0],vboxes[i][1],vboxes[i][2]);
vaabbs[i].extents = Vector(vboxes[i][3],vboxes[i][4],vboxes[i][5]);
}
openravepy::PythonThreadSaver threadsaver;
return _pbody->InitFromBoxes(vaabbs,bDraw,uri);
}

Expand All @@ -2745,13 +2765,15 @@ bool PyKinBody::InitFromSpheres(const boost::multi_array<dReal,2>& vspheres, boo
for(size_t i = 0; i < vvspheres.size(); ++i) {
vvspheres[i] = Vector(vspheres[i][0],vspheres[i][1],vspheres[i][2],vspheres[i][3]);
}
openravepy::PythonThreadSaver threadsaver;
return _pbody->InitFromSpheres(vvspheres,bDraw,uri);
}

bool PyKinBody::InitFromTrimesh(object pytrimesh, bool bDraw, const std::string& uri)
{
TriMesh mesh;
if( ExtractTriMesh(pytrimesh,mesh) ) {
openravepy::PythonThreadSaver threadsaver;
return _pbody->InitFromTrimesh(mesh,bDraw,uri);
}
else {
Expand All @@ -2769,6 +2791,7 @@ bool PyKinBody::InitFromGeometries(object ogeometries, const std::string& uri)
}
geometries[i] = pygeom->GetGeometryInfo();
}
openravepy::PythonThreadSaver threadsaver;
return _pbody->InitFromGeometries(geometries, uri);
}

Expand All @@ -2782,6 +2805,7 @@ void PyKinBody::InitFromLinkInfos(py::object olinkinfos, const std::string& uri)
}
linkInfos[i] = *pylinkinfo->GetLinkInfo();
}
openravepy::PythonThreadSaver threadsaver;
return _pbody->InitFromLinkInfos(linkInfos, uri);
}

Expand All @@ -2791,11 +2815,13 @@ bool PyKinBody::Init(object olinkinfos, object ojointinfos, const std::string& u
_ParseLinkInfos(olinkinfos, vlinkinfos);
std::vector<KinBody::JointInfoConstPtr> vjointinfos;
_ParseJointInfos(ojointinfos, vjointinfos);
openravepy::PythonThreadSaver threadsaver;
return _pbody->Init(vlinkinfos, vjointinfos, uri);
}

void PyKinBody::SetLinkGeometriesFromGroup(const std::string& geomname, const bool propagateGroupNameToSelfCollisionChecker)
{
openravepy::PythonThreadSaver threadsaver;
_pbody->SetLinkGeometriesFromGroup(geomname, propagateGroupNameToSelfCollisionChecker);
}

Expand All @@ -2814,6 +2840,7 @@ void PyKinBody::SetLinkGroupGeometries(const std::string& geomname, object olink
geometries[j] = pygeom->GetGeometryInfo();
}
}
openravepy::PythonThreadSaver threadsaver;
_pbody->SetLinkGroupGeometries(geomname, linkgeometries);
}

Expand Down

0 comments on commit 41af665

Please sign in to comment.