473,542 Members | 6,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multithreaded C API Python questions

Hi

A couple of mulithreaded C API python questions:

I) The PyGILState_Ensure() simply ensures python api call ability, it
doesnt actually lock the GIL, right?

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

II) Am I required to lock the GIL prior to running any python functions
(in a threaded app)?

PyThreadState *pts = PyGILState_GetThisThreadState();
PyEval_AcquireThread(pts);
PyObject_CallObject(...);

III) Shouldn't the GIL be released after 100 bytecodes or so to enable
other waiting threads to get the GIL?

I'm unable to get access to python as long as another python call is
executing. The PyEval_AcquireThread() call blocks until the first call
returns. I was hoping that the python system itself would release the
GIL after some execution, but it itsnt.

I am dependent upon the ability to have to threads executing in python
land at the same time. How can this be done?
Regards,
Svein Seldal
Nov 9 '06 #1
9 5812
Svein Seldal wrote:
Hi

A couple of mulithreaded C API python questions:

I) The PyGILState_Ensure() simply ensures python api call ability, it
doesnt actually lock the GIL, right?

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

II) Am I required to lock the GIL prior to running any python functions
(in a threaded app)?

PyThreadState *pts = PyGILState_GetThisThreadState();
PyEval_AcquireThread(pts);
PyObject_CallObject(...);

III) Shouldn't the GIL be released after 100 bytecodes or so to enable
other waiting threads to get the GIL?

I'm unable to get access to python as long as another python call is
executing. The PyEval_AcquireThread() call blocks until the first call
returns. I was hoping that the python system itself would release the
GIL after some execution, but it itsnt.

I am dependent upon the ability to have to threads executing in python
land at the same time. How can this be done?
PyGILState_Ensure/Release guarantees to establish the GIL - even if it was already held (useful if you deal with complex call ordering/dependencies)
PyObject_CallObject(...) calls Python code. Thus the interpreter will switch and do as usuall during that.
In your/C/system... code you are responsible to release the GIL or not to enable other Python threads (and prevent from deadlocks)
Usually you'd do this
* if you do time consuming C/system stuff
* or if the code can possibly renter Python through the system (e.g. when you call a Windows function which itself can create Windows messages to be routed back into Python message handlers)
-robert
Nov 9 '06 #2
robert wrote:
PyGILState_Ensure/Release guarantees to establish the GIL - even if it
was already held (useful if you deal with complex call
ordering/dependencies)
I understand this to mean that I dont need to explicitly lock the GIL
(with PyEval_AcquireLock() or PyEval_AcquireThread()).

Well I cant figure out the PyGILState_Ensure() because if I only use
this function to establish the GIL, when calling python, python will
shortly after crash with "Fatal Python error: ceval: tstate mix-up".
This happens consistently when the main app and the extra thread has
called python and both are busy executing python code.
PyObject_CallObject(...) calls Python code. Thus the interpreter
will switch and do as usuall during that.
BTW What do you mean by switch?
In your/C/system... code you are responsible to release the GIL or not
to enable other Python threads (and prevent from deadlocks) Usually
you'd do this * if you do time consuming C/system stuff
* or if the code can possibly renter Python through the system (e.g.
when you call a Windows function which itself can create Windows
messages to be routed back into Python message handlers)
The main problem is that not done this way, it's the other way around.
My main C app will call a python function which will be a lengthy time
consuming process.

The main C app will call python and it will never return from
PyObject_CallObject(...), while the extra thread should call a py
function to deliver occational messages into C.
Svein
Nov 9 '06 #3
Svein Seldal wrote:
I'm unable to get access to python as long as another python call is
executing. The PyEval_AcquireThread() call blocks until the first call
returns. I was hoping that the python system itself would release the
GIL after some execution, but it itsnt.

I am dependent upon the ability to have to threads executing in python
land at the same time. How can this be done?
Are you creating your threads through python or through C code? If you
are only creating it through C code, then make sure you initialize the
GIL with the following call at the beginning of your application:

PyEval_InitThreads()

From my experience, calling PyGILState_Ensure() was enough. I didn't
need to call any extra threading functions. But make sure that every
call to PyGILState_Ensure() is matched with a call to PyGILState_Release()

-Farshid
Nov 9 '06 #4
Svein Seldal wrote:
robert wrote:
>PyGILState_Ensure/Release guarantees to establish the GIL - even if it
was already held (useful if you deal with complex call
ordering/dependencies)

I understand this to mean that I dont need to explicitly lock the GIL
(with PyEval_AcquireLock() or PyEval_AcquireThread()).

Well I cant figure out the PyGILState_Ensure() because if I only use
this function to establish the GIL, when calling python, python will
shortly after crash with "Fatal Python error: ceval: tstate mix-up".
This happens consistently when the main app and the extra thread has
called python and both are busy executing python code.
Do did't tell enough to see what you want to do on the big picture.

usually you create a thread through Python.
If you boot Python at all from C you have to do
PyEval_InitThreads
or maybe more simple use the high level layer PyRun_...

If you boot a new Python thread manually or a separated interpreter (with separated module state) from outside
the you have to also do:

(PyInterpreterState* PyInterpreterState_New() )
PyThreadState* PyThreadState_New( PyInterpreterState *interp)
void PyEval_AcquireThread( PyThreadState *tstate)
If you are already in a Python thread but don't for some reason not know the current thread state:
PyThreadState* PyThreadState_Get( )
but usually you release with PyThreadState* PyEval_SaveThread( ) / Py_BEGIN_ALLOW_THREADS
>PyObject_CallObject(...) calls Python code. Thus the interpreter
will switch and do as usuall during that.

BTW What do you mean by switch?
the automatic scheduling every sys.getcheckinterval() etc.. you mentioned
You'd only have to take care for that, if your C-code does things (e.g. calling python basic funcs) for a long time and doesn't release the lock with Py_BEGIN_ALLOW_THREADS ...
The main problem is that not done this way, it's the other way around.
My main C app will call a python function which will be a lengthy time
consuming process.

The main C app will call python and it will never return from
PyObject_CallObject(...), while the extra thread should call a py
function to deliver occational messages into C.
Maybe simply boot Python in the main thread (Py_Initialize( )) and run off Python possibly as simple as PyRun_String
and let Python do thread.start_new(...)
You'd not have to worry much about states.

( and later you'd probably even more simple do "python myapp.py" and expose your C app as extension module :-) )
-robert
Nov 9 '06 #5
"Svein Seldal" <"svein at seldal dot com">wrote:
8<---------------------------------------------------
I am dependent upon the ability to have to threads executing in python
land at the same time. How can this be done?
call time.sleep(0.001) in each, as well as the main thread, to politely give the
rest a chance....

Fiddle with the sleep times in your setup to get the best performance - and
don't be misled into thinking that faster is better - its not always true -
there comes a point where the overhead of swapping a thread consumes the total
resource of the process, and no work gets done.

- HTH

- Hendrik

Nov 10 '06 #6
Hi!

I think I've found the bug, but I need to confirm this behavior.

My findings is that if you use PyEval_InitThreads(), it is crucial to
release the GIL with PyEval_ReleaseThread() afterwards.

The API docs states that you can release the GIL with
PyEval_ReleaseLock() or PyEval_ReleaseThread() AFAICS.
http://docs.python.org/api/threads.html under docs of void
PyEval_InitThreads().

However, if I do use PyEval_ReleaseLock() it will crash shortly after
with "Fatal Python error: ceval: tstate mix-up" in a multithreaded C
environment.

If I use PyEval_ReleaseThread() to release the GIL, my app seems stable.
I release the GIL like this:

PyThreadState *pts = PyGILState_GetThisThreadState();
PyEval_ReleaseThread(pts);

Now, is this a feature or expected behavior in python (i.e. unclear API
documentation), or is this a bug in python itself?
Regards,
Svein
PS:

For reference I did something like this in pseudo-code:

Py_Initialize();
PyEval_InitThreads();

other_py_inits(); // Load py modules, etc.

PyEval_ReleaseLock(); // <-- MAKES THE APP UNSTABLE

create_c_thread();

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

call_py_function_main(); // Py main() wont return

PyGILState_Release(gstate);
And the "main" of the C thread function looks like this:
while(1)
{
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

call_py_function_send();

PyGILState_Release(gstate);
}
Nov 14 '06 #7
Svein Seldal wrote:

You seem to use the functions in very rude manner - wanting to force the GIL around at lowest level.
Better forget the word GIL and think about acquiring and releasing Threads.
For each thread wanting to execute Python stuff there has to be a thread state (ts). And then you have to enter/leave for executing Python by using the thread state.
PyEval_InitThreads enters Python (incl. the GIL) first time.
To let Python free in main thread do
ts=PyEval_SaveThread() // or more easy: Py_BEGIN_ALLOW_THREADS

to reenter Python do simply:

PyEval_RestoreThread(ts) // or: Py_END_ALLOW_THREADS

Forget all the low level PyGIL... functions.

when you really want another c thread to enter Python first time with no thread state existing through Python itsel (better make the thread in Python with thread.start_new ?), then you have to do once

ts_cthread = PyThreadState_New(interp)

then enter Python in this thread:

PyEval_AcquireThread( ts_cthread )

to leave again:

PyEval_ReleaseThread( ts_cthread )
If you just loop the call_py_function_send() mainly in this thread you probably better create the thread at all in Python and make the loop there. You probably stick too much to C-level at any price :-) Probably you just do a PyRun_xxxx in main thread and then everything else in Python, and expose C-parts for the thread-loop to Python as function (in other_py_inits) - where in the c-function you probably have the usual Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS bracket during time consuming/system/reenter-endangered message stuff.
Robert


I think I've found the bug, but I need to confirm this behavior.

My findings is that if you use PyEval_InitThreads(), it is crucial to
release the GIL with PyEval_ReleaseThread() afterwards.

The API docs states that you can release the GIL with
PyEval_ReleaseLock() or PyEval_ReleaseThread() AFAICS.
http://docs.python.org/api/threads.html under docs of void
PyEval_InitThreads().

However, if I do use PyEval_ReleaseLock() it will crash shortly after
with "Fatal Python error: ceval: tstate mix-up" in a multithreaded C
environment.

If I use PyEval_ReleaseThread() to release the GIL, my app seems stable.
I release the GIL like this:

PyThreadState *pts = PyGILState_GetThisThreadState();
PyEval_ReleaseThread(pts);

Now, is this a feature or expected behavior in python (i.e. unclear API
documentation), or is this a bug in python itself?
Regards,
Svein
PS:

For reference I did something like this in pseudo-code:

Py_Initialize();
PyEval_InitThreads();

other_py_inits(); // Load py modules, etc.

PyEval_ReleaseLock(); // <-- MAKES THE APP UNSTABLE

create_c_thread();

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

call_py_function_main(); // Py main() wont return

PyGILState_Release(gstate);
And the "main" of the C thread function looks like this:
while(1)
{
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

call_py_function_send();

PyGILState_Release(gstate);
}
Nov 16 '06 #8
robert wrote:
Forget all the low level PyGIL... functions.'
Quite the contrary, I would say!

From the Py C API doc, I interpret the PyGIL... functions to be higher
leveled than eg. PyEval_SaveThread(). I've checked into the source of
python to find out what's really going on, and I've learnt a couple of
interesting facts. The PyGIL... are helper functions build over the
PyEval... functions, not the other way around.

A real beauty about the PyGILState_Ensure() is the fact that if it's
called from a thread that is (yet) unknown to python, it will actually
create the thread state object for you (with PyThreadState_New) and
acquire the lock. Hence, I dont have to worry about the process of
swapping thread states with PyEval_xxxThread() functions. I ONLY need to
use PyGILState_Ensure() prior to any py-ops.

From my previous post, I asked about the difference between
PyEval_ReleaseLock() and PyEval_ReleaseThread() and I've found one major
difference. Both release the GIL as the docs states, but the
PyEval_ReleaseThread() saves the current thread state as well. When I
used PyEval_ReleaseLock() this didnt happen, preventing proper saving of
the current thread, causing py crash when control were handed back to
this thread.

By using:

PyEval_InitThreads();
py_ops();
PyThreadState *pts = PyGILState_GetThisThreadState();
PyEval_ReleaseThread(pts);

And for each py-op later on (from arbitrary thread):

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
py_ops();
PyGILState_Release(gstate);

Then you're home free. Everything related to threading is handled by
Py/API itself.
Probably you just do a PyRun_xxxx in main thread and then everything
else in Python, and expose C-parts for the thread-loop to Python as
function (in other_py_inits) - where in the c-function you probably have
the usual Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS bracket during
time consuming/system/reenter-endangered message stuff.
The app i'm building is a plugin to a server system, and I cant control
nor remove any threads that the server uses. I am given one main thread
to run python forever, and messages that are to be delivered into python
are called from another.

I could do it like you propose: The data coming from my server will
arrive into a c-function called by a server thread. The c-part of the
thread-loop would then be run as another thread (started from python).
Its fully feasible, yet the challenge is to make proper data sync-ing
between these two threads.

Well, I think I've achieved what I wanted. Python seems apparently
stable, and reading from the python sources, I cant see any immediate
reasons why it shouldn't.

Thanks for letting me closer to a working solution!
Regads,
Svein
Nov 16 '06 #9
Svein Seldal wrote:
A real beauty about the PyGILState_Ensure() is the fact that if it's
called from a thread that is (yet) unknown to python, it will actually
create the thread state object for you (with PyThreadState_New) and
acquire the lock. Hence, I dont have to worry about the process of
swapping thread states with PyEval_xxxThread() functions. I ONLY need to
use PyGILState_Ensure() prior to any py-ops.
Interesting - the PyGILState_Ensure was new to py2.3. I didn't notice so far, that it will even auto-create a fresh tread state (from default InterpreterState only). From the docs its not that clear.

The confusion in usage is that one has to do

Py_Initialize()
PyEval_InitThreads()
ts=PyEval_SaveThread()

in order to start clean for further pure "meta-high-level" PyGILState_Ensure/Release.

They obviously meant PyGILState_Ensure to make live easier, but the discussion shows, it adds additional worries probably forcing each user into investigating the Python C-code :-)

There should probably a extra init function for this meta-high-level usage like ..

PyGILState_PyInitThreads() // which would not pre-acquire the lock! - as anyone expects to enter then with PyGILState_Ensure

.. and these 3 Functions grouped together in the docs for an easy-to-use meta-high-level thread state API.

The current thread state API doc, as you read it from top to bottom now, is in fact totally confusing for anyone who didn't develop Python himself :-)
Robert
Nov 17 '06 #10

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

Similar topics

2
2275
by: paul h | last post by:
hi there, i'm learning to use python for cgi, and i've hit a few road blocks. basically, i need to know how to achieve the following things using python, on linux. create a new directory, and sub-directory. so if the user inputs "HELLO", i can create a dir called HELLO, and then some other dirs in there. i also need to know how i can...
3
1291
by: Eli Daniel | last post by:
Hi, I am relative new to Python. Please tell me if the following is possible. I have a command line shell written in C. This shell executes some commands which I would like to import to the python shell. Here is an example command from my shell: openconnection www.yahoo.com 12 2000 So I've created a dll (myshell_d.dll) according to the...
0
1000
by: Derek Allen | last post by:
I'm using SWIG to generate glue code for my embedded/extended python app. There are a couple of very frustrating issues that I would like to see if anyone else has solutions for: - Once I have created and initialized a module with SWIG I can call it by executing a python file with no problems. However I would like to be able to type in a...
3
5059
by: Leonard J. Reder | last post by:
Hello list, I have been searching on the web for a while now for a specific Python implementation of an FSM. More specifically what I am looking for is a Python implementation of the so called UML Harel State Machine notion of a state machine. These are multi-threaded capable state machines with hierarchical representation capability...
0
1413
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application runnig on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird behaviors). Can anyone clarify:
1
1762
by: notanotheridiot | last post by:
Hi- I'm trying to exec some arbitrary code in one thread of an application and read anything it prints to stdout or stderr in another thread. My question is how? I've tried changing sys.stdout, but that changes stdout for the whole application, not just that one thread, which means that any status updates that I DO want printed to stdout...
21
1543
by: nateastle | last post by:
I have a simple assignment for school but am unsure where to go. The assignment is to read in a text file, split out the words and say which line each word appears in alphabetical order. I have the basic outline of the program done which is: def Xref(filename): try: fp = open(filename, "r") lines = fp.readlines() fp.close()
3
1667
by: johnny | last post by:
I have python script does ftp download in a multi threaded way. Each thread downloads a file, close the file, calls the comman line to convert the .doc to pdf. Command line should go ahead and convert the file. My question is, when each thread calls the command line, does one command line process all the request, or each thread creates a one...
6
1128
by: LessPaul | last post by:
I recently discovered Python and see it as a great language to use for personal projects (and more). I made my living for over a decade as a coder in C, C++, ADA, Fortran, and Assembly before moving to systems engineering. I'm now retired, and would love to code again. I see Python as the perfect language to get a good program working in a...
0
7567
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7665
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5862
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5239
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4878
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3377
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1790
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.