2

I'm embedding Python inside my C# application trough pythonnet https://github.com/pythonnet/pythonnet

I'm spawning worker threads (one at a time), that spawn python subinterpreter do some work in isolated environment and end sub intepreter. Everything worked fine, until I used pymongo, and after that Py_EndInterpreter started to fail.

Py_EndInterpreter(PyThreadState *tstate)
{
    PyInterpreterState *interp = tstate->interp;

    if (tstate != PyThreadState_GET())
        Py_FatalError("Py_EndInterpreter: thread is not current");
    if (tstate->frame != NULL)
        Py_FatalError("Py_EndInterpreter: thread still has a frame");
    if (tstate != interp->tstate_head || tstate->next != NULL)
        Py_FatalError("Py_EndInterpreter: not the last thread");

    PyImport_Cleanup();
    PyInterpreterState_Clear(interp);
    PyThreadState_Swap(NULL);
    PyInterpreterState_Delete(interp);
}

It fails on

if (tstate != interp->tstate_head || tstate->next != NULL)
    Py_FatalError("Py_EndInterpreter: not the last thread");

And right now i really don't have an idea how to handle that and make the code working. Very short version of what I'm doing, at is failing is

            Runtime.Py_Initialize();
            Runtime.PyEval_InitThreads();

            IntPtr thread_state = Runtime.PyEval_SaveThread();


            IntPtr gil = Runtime.PyGILState_Ensure();

            int i = 0;
            while (i < 5)
            {
                AutoResetEvent resetEvent = new AutoResetEvent(false);               
                BackgroundWorker worker = new BackgroundWorker();
                worker.DoWork += (o, ea) =>
                {
                    Thread.Sleep(2000);
                    IntPtr interpreter = Runtime.Py_NewInterpreter();

                    string str = @"
import pymongo
client = pymongo.MongoClient(""localhost"")";
                    Runtime.PyRun_SimpleString(str);
                    Runtime.Py_EndInterpreter(interpreter);

                    resetEvent.Set();
                };
                worker.RunWorkerAsync();
                resetEvent.WaitOne();

                i++;
            }

            Runtime.PyThreadState_Swap(thread_state);
            Runtime.PyGILState_Release(gil);

            Runtime.PyEval_RestoreThread(thread_state);
            Runtime.Py_Finalize();

1 Answer 1

3

It looks like pymongo creates own threads (for connection pool) and in this case it's more difficult to control interpreter's state and shutdown it gracefully. On the other hand, try to do this in the script itself:

                    string str = @"
import pymongo
client = pymongo.MongoClient(""localhost"")
client.close()";

Doc says:

close()

Disconnect from MongoDB.

Close all sockets in the connection pools and stop the monitor threads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.