Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
ecdd8689
Commit
ecdd8689
authored
Dec 07, 2015
by
Wenzel Jakob
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
documentation on using the gil
parent
24fe0904
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
2 deletions
+54
-2
docs/advanced.rst
+53
-1
docs/conf.py
+1
-1
No files found.
docs/advanced.rst
View file @
ecdd8689
...
@@ -300,6 +300,59 @@ a virtual method call.
...
@@ -300,6 +300,59 @@ a virtual method call.
demonstrates how to override virtual functions using pybind11 in more
demonstrates how to override virtual functions using pybind11 in more
detail.
detail.
Global Interpreter Lock (GIL)
=============================
The classes :class:`gil_scoped_release` and :class:`gil_scoped_acquire` can be
used to acquire and release the global interpreter lock in the body of a C++
function call. In this way, long-running C++ code can be parallelized using
multiple Python threads. Taking the previous section as an example, this could
be realized as follows (important changes highlighted):
.. code-block:: cpp
:emphasize-lines: 8,9,33,34
class PyAnimal : public Animal {
public:
/* Inherit the constructors */
using Animal::Animal;
/* Trampoline (need one for each virtual function) */
std::string go(int n_times) {
/* Acquire GIL before calling Python code */
gil_scoped_acquire acquire;
PYBIND11_OVERLOAD_PURE(
std::string, /* Return type */
Animal, /* Parent class */
go, /* Name of function */
n_times /* Argument(s) */
);
}
};
PYBIND11_PLUGIN(example) {
py::module m("example", "pybind11 example plugin");
py::class_<PyAnimal> animal(m, "Animal");
animal
.alias<Animal>()
.def(py::init<>())
.def("go", &Animal::go);
py::class_<Dog>(m, "Dog", animal)
.def(py::init<>());
m.def("call_go", [](Animal *animal) -> std::string {
/* Release GIL before calling into (potentially long-running) C++ code */
gil_scoped_release release;
return call_go(animal);
});
return m.ptr();
}
Passing STL data structures
Passing STL data structures
===========================
===========================
...
@@ -748,4 +801,3 @@ When conversion fails, both directions throw the exception :class:`cast_error`.
...
@@ -748,4 +801,3 @@ When conversion fails, both directions throw the exception :class:`cast_error`.
The file :file:`example/example2.cpp` contains a complete example that
The file :file:`example/example2.cpp` contains a complete example that
demonstrates passing native Python types in more detail.
demonstrates passing native Python types in more detail.
docs/conf.py
View file @
ecdd8689
...
@@ -118,7 +118,7 @@ if not on_rtd: # only import and set the theme if we're building docs locally
...
@@ -118,7 +118,7 @@ if not on_rtd: # only import and set the theme if we're building docs locally
html_theme_path
=
[
sphinx_rtd_theme
.
get_html_theme_path
()]
html_theme_path
=
[
sphinx_rtd_theme
.
get_html_theme_path
()]
html_context
=
{
html_context
=
{
'css_files'
:
[
'css_files'
:
[
'
_
static/theme_overrides.css'
,
'
.
static/theme_overrides.css'
,
]
]
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment