473,466 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Create 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 2290
In article <pU**************@jessikat.fsnet.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**@pythoncraft.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**********@panix3.panix.com>, Aahz <aa**@pythoncraft.com>
writes
In article <pU**************@jessikat.fsnet.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_ParseTuple 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.intevation.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_ParseTuple 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.fsnet.co.uk>,
Robin Becker <ro***@jessikat.fsnet.co.uk> wrote:
In article <bl**********@panix3.panix.com>, Aahz <aa**@pythoncraft.com>
writes
In article <pU**************@jessikat.fsnet.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 "accelerator" you were already releasing
the GIL.
--
Aahz (aa**@pythoncraft.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**********@panix3.panix.com>, Aahz <aa**@pythoncraft.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 "accelerator" 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
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...
7
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...
1
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...
5
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...
4
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...
0
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. ...
0
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^...
5
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...
4
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:...
0
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...
0
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,...
0
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...
1
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...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.