473,320 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Py_NewInterpreter and PyGILState API

Hello,

i have some trouble to understand the Py_NewInterpreter API function. The
docs state, that Py_NewInterpreter returns a new PyThreadState instance. One
can switch between interpreters by swapping thread states. I've used that,
and it worked well, until i decided to use the PyGILState_* API functions.

After digging a while, i found, that these functions assume, that there is
only one PyThreadState instance per thread. However, this doesn't play nice
with using the Py_NewInterpreter function, which always returns a new thread
state (as i understand it), even if used in a single-threaded application.

I'm wondering, why Py_NewInterpreter does actually return a PyThreadState
pointer instead of a PyInterpreterSate pointer?

My current solution uses the following code to swap between interpreters:

// i hold an 'is' variable for each interpreter i need:
PyInterpreterState *is = Py_NewInterpreter()->interp;

// to switch to a specific interpreter:
PyGILState_STATE gilState = PyGILState_Ensure();
PyThreadState_Get()->interp = is;
// use python API
PyGILState_Release(gilState);

This code seems to work fine, but one issue remains: I can't use

PyGILState_STATE gilState = PyGILState_Ensure();
Py_EndInterpreter(PyThreadState_Get());
PyGILState_Release(gilState);

I get the following error: "Fatal Python error: Py_EndInterpreter: not the
last thread". I guess this is due to the fact that i throw away the
PyThreadState returned by Py_NewInterpreter ?

Thanks in advance,
Christoph

Jul 18 '05 #1
0 2103

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: freesteel | last post by:
/* Is this a bug in Py_NewInterpreter? The function below "MyThread" is instantiated from a windows worker thread, but I don't think that is relevant. (I can try this on a linux box, but I...
62
by: robert | last post by:
I'd like to use multiple CPU cores for selected time consuming Python computations (incl. numpy/scipy) in a frictionless manner. Interprocess communication is tedious and out of question, so I...
0
Bulldog
by: Bulldog | last post by:
I am working on a C++ win32 DLL which has calls to python, mainly import some modules, call some functions and return some values. Right now I am using the Py_Initialize() and Py_Uninitialize()...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.