Skip to content

πŸŽ› πŸ”Š A Python library for audio.

Add explicit Numpy import to workaround GIL deadlock with asyncio. (#155)

Peter Sobot40275fa8389d
diff --git a/pedalboard/__init__.py b/pedalboard/__init__.py
index 7b83ab7..39a9d45 100644
--- a/pedalboard/__init__.py
+++ b/pedalboard/__init__.py
@@ -14,6 +14,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+# In certain situations (including when using asyncio), Pedalboard's C++ code will
+# implicitly and lazily import NumPy via its Pybind11 bindings. This lazy import
+# can sometimes (!) cause a deadlock on the GIL if multiple threads are attempting
+# it simultaneously. To work around this, we preload Numpy into the current Python
+# process so that Pybind11's lazy import is effectively a no-op.
+import numpy # noqa
+
try:
from pedalboard_native import * # noqa: F403, F401
except ImportError as _e:
diff --git a/pedalboard/io/__init__.py b/pedalboard/io/__init__.py
index a40dee2..8eaf2bb 100644
--- a/pedalboard/io/__init__.py
+++ b/pedalboard/io/__init__.py
@@ -1 +1,7 @@
+# In certain situations (including when using asyncio), Pedalboard's C++ code will
+# implicitly and lazily import NumPy via its Pybind11 bindings. This lazy import
+# can sometimes (!) cause a deadlock on the GIL if multiple threads are attempting
+# it simultaneously. To work around this, we preload Numpy into the current Python
+# process so that Pybind11's lazy import is effectively a no-op.
+import numpy # noqa
from pedalboard_native.io import * # noqa: F403, F401