473,672 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extension call backs and the GIL

I'm trying to better my understanding of multithreading. I believe a
thread that calls an extension must have the GIL. What happens if the
extension then calls back into Python? Can a function or method somehow
'belong' to a different thread? My understanding was that things should
be OK provided the extension code doesn't mess with the GIL (which it
doesn't).

I'm asking because the ReportLab tool kit contains an accelerator for a
stringWidth function that relies on a callback to fill in some global
data upon demand. A user reports thread problems when using the
accelerator.
--
Robin Becker
Jul 18 '05 #1
6 2296
In article <pU************ **@jessikat.fsn et.co.uk>,
Robin Becker <ro***@jessikat .fsnet.co.uk> wrote:

I'm trying to better my understanding of multithreading. I believe a
thread that calls an extension must have the GIL. What happens if the
extension then calls back into Python? Can a function or method somehow
'belong' to a different thread? My understanding was that things should
be OK provided the extension code doesn't mess with the GIL (which it
doesn't).


You need to reacquire the GIL prior to making the callback.
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
Jul 18 '05 #2
In article <bl**********@p anix3.panix.com >, Aahz <aa**@pythoncra ft.com>
writes
In article <pU************ **@jessikat.fsn et.co.uk>,
Robin Becker <ro***@jessikat .fsnet.co.uk> wrote:

I'm trying to better my understanding of multithreading. I believe a
thread that calls an extension must have the GIL. What happens if the
extension then calls back into Python? Can a function or method somehow
'belong' to a different thread? My understanding was that things should
be OK provided the extension code doesn't mess with the GIL (which it
doesn't).


You need to reacquire the GIL prior to making the callback.

So does calling an extension release the GIL? I believed the opposite
for some reason.
--
Robin Becker
Jul 18 '05 #3
Robin Becker <ro***@jessikat .fsnet.co.uk> writes:
You need to reacquire the GIL prior to making the callback.

So does calling an extension release the GIL?


No. It would be very inconvenient if it did because what most functions
do first is to call one of the PyArg_ParseTupl e functions which could
execute arbitrary python code.

I think it would help if you described a bit more what the actual
problem is or what the user does.

I looked briefly at the sources and assuming tha the accelerator in
question is lib/_rl_accel.c and the callback whatever has been assigned
to _SWRecover, could it be that the problem is that your user tries to
use different callbacks in different threads? _SWRecover is a global
after all and shared by all threads.

Bernhard

--
Intevation GmbH http://intevation.de/
Sketch http://sketch.sourceforge.net/
Thuban http://thuban.intevation.org/
Jul 18 '05 #4
In article <6q************ @salmakis.intev ation.de>, Bernhard Herzog
<bh@intevation. de> writes
Robin Becker <ro***@jessikat .fsnet.co.uk> writes:
You need to reacquire the GIL prior to making the callback. So does calling an extension release the GIL?


No. It would be very inconvenient if it did because what most functions
do first is to call one of the PyArg_ParseTupl e functions which could
execute arbitrary python code.


Thank goodness for that :)

I think it would help if you described a bit more what the actual
problem is or what the user does.

I looked briefly at the sources and assuming tha the accelerator in
question is lib/_rl_accel.c and the callback whatever has been assigned
to _SWRecover, could it be that the problem is that your user tries to
use different callbacks in different threads? _SWRecover is a global
after all and shared by all threads.
I'm not certain, but yes I think that's probably what the problem comes
down to; if so I can claim there are already too many breakages in our
font system to worry about that :(

Bernhard


--
Robin Becker
Jul 18 '05 #5
In article <OW************ **@jessikat.fsn et.co.uk>,
Robin Becker <ro***@jessikat .fsnet.co.uk> wrote:
In article <bl**********@p anix3.panix.com >, Aahz <aa**@pythoncra ft.com>
writes
In article <pU************ **@jessikat.fsn et.co.uk>,
Robin Becker <ro***@jessikat .fsnet.co.uk> wrote:

I'm trying to better my understanding of multithreading. I believe a
thread that calls an extension must have the GIL. What happens if the
extension then calls back into Python? Can a function or method somehow
'belong' to a different thread? My understanding was that things should
be OK provided the extension code doesn't mess with the GIL (which it
doesn't).


You need to reacquire the GIL prior to making the callback.


So does calling an extension release the GIL? I believed the opposite
for some reason.


Looks like Bernhard has put you on the right track, but just for the
record: calling an extension does not automatically release the GIL; you
need to explicitly call the Py_BEGIN_ALLOW_ THREADS macro. I was just
assuming that because you said "accelerato r" you were already releasing
the GIL.
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
Jul 18 '05 #6
In article <bl**********@p anix3.panix.com >, Aahz <aa**@pythoncra ft.com>
writes
.......
Looks like Bernhard has put you on the right track, but just for the
record: calling an extension does not automatically release the GIL; you
need to explicitly call the Py_BEGIN_ALLOW_ THREADS macro. I was just
assuming that because you said "accelerato r" you were already releasing
the GIL.

The code is intended to run single thread so we never have done anything
about threading. It's a real pain. In a zope world we ought really to
give every request a fresh context ie each thread would see an entirely
'clean' python and then be forced to load its own fonts/resources. It
just sucks from the overall system viewpoint. How does one ensure that
each thread sees its own version of sys.modules etc etc or is that
considered a dumb question?
--
Robin Becker
Jul 18 '05 #7

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

Similar topics

3
1715
by: Ever Olano | last post by:
I failed to include this in my initial post. I apologize. Can a PHP extension call/use another PHP extension? If so, someone please point me to the right docs/sites or if it's short enough to post the info in your reply, that would be great too. Thanks, Ever
7
6073
by: VK | last post by:
Hello: Since our application uses ASP, I am posting this in this group. Pardon if wrong group. Our product/application replies a lot on bounced back emails, that way we correct this and let the client know. Off late we are receiving very, very few of these emails. It turns out, with amount of spamming going on most of the companies have set up filters. As a result, most of emails, even though incorrect don't get bounced back.
1
5721
by: David C. allen | last post by:
I have created a simple Client-side SOAP Extension for a webclass that I have. When I apply the extension attribute to the the calling function in the proxy class I get an error 'Value cannot be null'. When the extension attribute is not applied it runs fine. The wierd thing is that it does not appear to be an error within the SOAP extension because I break-pointed it and when the proxy function gets called the SOAP extension runs fine....
5
6532
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly new to VB.NET, so I need some help. Here is a code snippit :
4
6020
by: pepcag | last post by:
I used http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconalteringsoapmessageusingsoapextensions.asp as a template to create a very simple web method with soap extension. The code like this: public string HelloWorld() { return "Hello World.";
0
992
by: dotnet_nut | last post by:
I need to populate a combo-box based upon the selection from another combo-box without postbacks. I know its possible using asp.net 2.0 client call backs. Any samples in Vb.Net will be helpful. Cheers !!!!!
0
1010
by: culubalo | last post by:
Ok so this has been annoying me since yesterday but should be simple enough to do. If I create, say a picture box in an array, in code in Form1.h as follows: System::Windows::Forms::PictureBox^ abc and load it up with a picture and draw it to form1 through code how can I handle call backs like clicking? I know theres a PictureBox::OnClick method but I have NO idea on how to use it and even if it is for what I want.
5
2024
by: malkarouri | last post by:
Hi everyone, Is it possible to write a Python extension that uses the Boehm garbage collector? I have a C library written that makes use of boehm-gc for memory management. To use that, I have to call GC_INIT() at the start of the program that uses the library. Now I want to encapsulate the library as a CPython extension. The question is really is that possible? And will there be conflicts between the boehm-gc and Python memory...
4
3356
by: Steffen Bobek | last post by:
Extension methods are made for use with instances. I'd like to "misuse" them as static methods, too. Let me tell you my ambition: I use an extension method to serialize objects somehow like this: MyObject obj = new MyObject(); obj.ToXmlFile("some directory\\some file.xml"); The method "ToXmlFile" is defined elsewhere far apart from MyObject to work for all objects:
0
8828
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8608
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
8680
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
7446
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...
1
6238
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5705
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4418
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
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
2
1816
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.