473,769 Members | 8,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extension module, thread safety?

Hi,

i write an extension module in C at the moment. This
module does some work on some own data types that
consist of some values.
The functions that can change the data are written in C.

The question came up if this is by itself thread safe,
if some two or more threads try to change these data types,
are the C functions by themselves are "atomic" or can they
be interrupted be the perl interpreter and then (data types
are in some inconsistent half-changed state) another function
that works on these data is called?
Thanks for hints,
Torsten.

Jul 18 '05 #1
8 2760
Torsten Mohr <tm***@s.netic. de> writes:
The question came up if this is by itself thread safe,
if some two or more threads try to change these data types,
are the C functions by themselves are "atomic" or can they
be interrupted be the perl interpreter and then (data types
are in some inconsistent half-changed state) another function
that works on these data is called?


I presume you mean "Python" and not "perl"...

If the threads under discussion are all Python threads, then by
default yes, the extension module C functions will appear to be atomic
from the perspective of the Python code. When the Python code calls
into the extension module, the GIL (global interpreter lock) is still
being held. Unless the extension module code explicitly releases the
GIL, no other Python threads can execute (even though those threads
are in fact implemented as native platform threads).

So in general, if you write an extension module where none of its
functions ever release the GIL, there's no way for two of its
functions to be run from different Python threads simultaneously.

Note that this restriction won't necessarily hold if there are other
ways (at the C level, or from other extension modules) to trigger code
in the extension module, since that's outside of the control of the
Python GIL. Nor will it necessarily hold true if your extension
module calls back out into Python (as a callback, or whatever) since
once the interpreter is back in Python code the interpreter itself
will periodically release the GIL, or some other extension code that
the callback code runs may release it.

To the extent possible, it's considered good practice to release the
GIL in an extension module whenever you are doing lengthy processing
so as to permit other Python threads (that may have nothing to do with
using your extension module) to execute. For short routines this
really isn't an issue, but if your extension module will be spending
some time managing its data, you may wish to add some internal thread
protection around that data, so that you can use your own locks rather
than depending on the GIL.

-- David
Jul 18 '05 #2
David Bolen a écrit :

If the threads under discussion are all Python threads, then by
default yes, the extension module C functions will appear to be atomic
from the perspective of the Python code. When the Python code calls
into the extension module, the GIL (global interpreter lock) is still
being held. Unless the extension module code explicitly releases the
GIL, no other Python threads can execute (even though those threads
are in fact implemented as native platform threads).


Indeed, there is this (so annoying) GIL ... is there any project about
extracting the (few ?) critical points of the python interpreter to put
locks around them instead of choosing the opposite strategy (ie. locking
anythime but when we know the section is not critical) ? Because it
would made embedding a python interpreter in another language much more
easy !

With the current CPython, it's very hard to mix Python and C in a
multithreading application (with C-threads, not Python-threads). In fact
I never really succeeded in that task because of that GIL ! I have a
multi-thread application but every bit of Python code must be run into a
Python thread. To be more precise, I wanted to be able to call Python
code in response to some GUI events, and I didn't want to instanciate a
new interpreter for I wanted to be able to access the environment of my
main Python interpreter.

By the way, if someone succeeded in a similar task, I'll be happy to
hear about it :)

Pierre

PS: if the main code is in C (in fact C++ ...) and not Python it's for
historical reasons of course ;)
Jul 18 '05 #3
Pierre Barbier de Reuille wrote:
With the current CPython, it's very hard to mix Python and C in a
multithreading application (with C-threads, not Python-threads). In fact
I never really succeeded in that task because of that GIL ! I have a
multi-thread application but every bit of Python code must be run into a
Python thread. To be more precise, I wanted to be able to call Python
code in response to some GUI events, and I didn't want to instanciate a
new interpreter for I wanted to be able to access the environment of my
main Python interpreter.


I don't understand. This is what PyGILState_Ensu re and PyGILState_Rele ase are
for - so C code can leave the GIL unlocked by default, and only grab it when
they want to call into the C/Python API.

Regards,
Nick.

--
Nick Coghlan | nc******@email. com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #4
Nick Coghlan a écrit :
Pierre Barbier de Reuille wrote:
With the current CPython, it's very hard to mix Python and C in a
multithreading application (with C-threads, not Python-threads). In
fact I never really succeeded in that task because of that GIL ! I
have a multi-thread application but every bit of Python code must be
run into a Python thread. To be more precise, I wanted to be able to
call Python code in response to some GUI events, and I didn't want to
instanciate a new interpreter for I wanted to be able to access the
environment of my main Python interpreter.

I don't understand. This is what PyGILState_Ensu re and
PyGILState_Rele ase are for - so C code can leave the GIL unlocked by
default, and only grab it when they want to call into the C/Python API.

Regards,
Nick.


Ok, I wondered why I didn't know these functions, but they are new to
Python 2.4 ( and I didn't take the time to look closely at Python 2.4 as
some modules I'm working with are still not available for Python 2.4).
But if it really allows to call Python code outside a Python thread ...
then I'll surely use that as soon as I can use Python 2.4 :) Thanks for
the hint :)

Pierre
Jul 18 '05 #5
Pierre Barbier de Reuille wrote:
Ok, I wondered why I didn't know these functions, but they are new to
Python 2.4 ( and I didn't take the time to look closely at Python 2.4 as
some modules I'm working with are still not available for Python 2.4).
But if it really allows to call Python code outside a Python thread ...
then I'll surely use that as soon as I can use Python 2.4 :) Thanks for
the hint :)


The Python 2.4 docs claim the functions were added in Python 2.3, even though
they aren't documented in the 2.3.4 docs.

The 2.3 release PEP (PEP 283) confirms that PEP 311 (which added these
functions) went in.

Cheers,
Nick.

--
Nick Coghlan | nc******@email. com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #6
Nick Coghlan a écrit :
The Python 2.4 docs claim the functions were added in Python 2.3, even
though they aren't documented in the 2.3.4 docs.

The 2.3 release PEP (PEP 283) confirms that PEP 311 (which added these
functions) went in.
Indeed, I just tested it and now it works fine :) Thanks a lot :)

Cheers,
Nick.

Jul 18 '05 #7
Nick Coghlan <nc******@iinet .net.au> writes:
Pierre Barbier de Reuille wrote:
Ok, I wondered why I didn't know these functions, but they are new
to Python 2.4 ( and I didn't take the time to look closely at Python
2.4 as some modules I'm working with are still not available for
Python 2.4). But if it really allows to call Python code outside a
Python thread ... then I'll surely use that as soon as I can use
Python 2.4 :) Thanks for the hint :)


The Python 2.4 docs claim the functions were added in Python 2.3, even
though they aren't documented in the 2.3.4 docs.

The 2.3 release PEP (PEP 283) confirms that PEP 311 (which added these
functions) went in.


And even before that it was certainly possible to call into the Python
interpreter from a native thread using existing functions, albeit the
newer functions are more convenient (and perhaps more robust, I don't
know).

My earliest interaction with Python (~1999, while writing a module
that extended and embedded Python 1.5.2) used PyEval_AcquireT hread()
and PyEval_ReleaseT hread() to get access to a thread state from a
native C application thread (not initiated by the Python interpreter)
to allow me to call safely into an executing Python script upon
asynchronous data reception by the C code.

-- David
Jul 18 '05 #8
David Bolen a écrit :
Nick Coghlan <nc******@iinet .net.au> writes:

And even before that it was certainly possible to call into the Python
interpreter from a native thread using existing functions, albeit the
newer functions are more convenient (and perhaps more robust, I don't
know).

My earliest interaction with Python (~1999, while writing a module
that extended and embedded Python 1.5.2) used PyEval_AcquireT hread()
and PyEval_ReleaseT hread() to get access to a thread state from a
native C application thread (not initiated by the Python interpreter)
to allow me to call safely into an executing Python script upon
asynchronous data reception by the C code.

-- David


Yes, now you mention this I remember trying to use these functions ! But
there is two problems with them (and only one is solved by the new
functions) ! The first problem is the dead lock is you try to acquire
the same thread twice. This implies a very low level use of these
function, and it can be a real pain to find where to put them without
risking this dead lock. This is no more a problem with the new
functions. But the second (and not solved) problem is: these functions
block the current thread when you try to get the GIL ! This is an issue
if you don't want your GUI thread to be blocked when a buggy module does
not release the GIL during long computations. I didn't find any function
like "test the state of the GIL and if it's free, then acquire it" !
And, IMO, it's missing ... Then, if python goes to a model where
critical sections are identified and the GIL acquired only there, all
these problems will disappear ! That's why I really hope Python 3 will
include this kind of thread support ...

Pierre
Jul 18 '05 #9

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

Similar topics

36
2641
by: Tim Churches | last post by:
If a compiled Python extension module B includes code from some other software A which is licensed only under the GPL, do other Python programmes, C, which import module B also need to be licensed under a GPL-compatible license (assuming C is/are to be distributed to third parties)? I think the answer is yes, both from a lay (IANAL) legal and moral point-of-view, but I am interested in hearing any arguments to the contrary. Note that I...
0
1373
by: Mark English | last post by:
Basic problem: If there is a C-extension module in a package and it tries to import another python module in the same package without using the fully qualified path, the import fails. Config: Python 2.4 on Windows 2000 For example: mypackage contains:
0
606
by: phpnube | last post by:
I used this php package to install on my system... PHP 5.0.0 installer - 26 July 2004 (CGI only, packaged as Windows installer to install and configure PHP, and automatically configure IIS, PWS and Xitami, with manual configuration for other servers. N.B. no external extensions included It works fine... as the test page outputs the correct php page. I installed PHP to make use of the mysql database thrue phpmyadmin. When i
1
12361
by: browntown | last post by:
I'm trying to install the Image_Color pear package. I keep getting the following error: "'gd' PHP extension is not installed" phpinfo on my web server indicates that gd is enabled. when I try to get info from my command line interface I don't see anything where it was compiled with gd. Am I right in assuming there are two ini files? One for the command
0
1223
by: fatslow | last post by:
I've been trying to update php and have installed php 5.1.6 on slackware 11 and previously on 10.2 but when I run 'pear config-test' I keep on getting a message: PHP Warning: PHP Startup: Unable to load dynamic library '...mysql.so' - '...mysql.so': undefined symbol: empty_string Unknown on line 0 PHP Warning: PHP Startup: (null): Unable to initialilze module Module compiled with module API=20020429, debug=0, thread-safety=0 PHP...
4
5806
by: Al Norman | last post by:
We have two separate DLLs that do not interact (directly, at least). One is an MFC extension DLL that was built back in VC++ 6 days (but has been recompiled with VS2005). The other is a DLL that contains .Net functions wrapped in C++ (as described by Paul DiLascia -- see http://msdn.microsoft.com/msdnmag/issues/06/06/CAtWork/default.aspx). Both DLLs specify 'Use MFC in a shared DLL'. Since we have an old VC 6 application (large) that we...
5
1644
by: Adam Atlas | last post by:
Does anyone know if it would be possible to create a CPython extension -- or use the ctypes module -- to access Python's own embedding API (http://docs.python.org/api/initialization.html &c.)? Could a Python program itself create a sub-interpreter, and work with it with all the privileges and capabilities that an actual C program would have? I realize that this may be a bit too... mystical? ... for a lot of people's tastes, but I'm just...
0
1149
by: pianomaestro | last post by:
I have a cython extension module "mux" where the initialization is being called more than once (the initmux c function). I am wondering if my use of multiple threads causes this. Can't multiple threads share the same python module ? The threads are being created from an external C library. I just call PyEval_InitThreads on startup, and then acquire the gil from the c callbacks.
1
1647
by: =?ISO-8859-1?Q?J=E9r=E9my_Wagner?= | last post by:
Hi, I recently tried to use the subprocess module within a threading.Thread class, but it appears the module is not thread-safe. What is the policy of python regarding thread-safety of a module ?
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5310
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
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

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.