Skip to content

Runtime error loading numpy #5171

Closed Answered by nkr0
nkr0 asked this question in Q&A
Discussion options

You must be logged in to vote

This issue comes from Py_FinalizeEx. Basically, initialising and finalising python interpreter multiple times from a process can lead to undefined behaviour.

Some extensions may not work properly if their initialization routine is called more than once; this can happen if an application calls Py_Initialize() and Py_FinalizeEx() more than once.

My solution at this point is to use a single scoped_interpreter and use PyDict_Clear to clean variables from it. Not at all thread safe.

#include <pybind11/embed.h>

namespace py = pybind11;

void test(const char *code) {
  // Clear references to modules and variables from `__main__`
  PyDict_Clear(PyModule_GetDict(PyImport_AddModule("__main__")))…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by nkr0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants