Threading a lengthy C function
Thomas Heller
theller at python.net
Thu Nov 20 14:02:44 EST 2003
[Thomas]
>>If the original posters intent is simply to call functions in a dll, he
>>should probably try out ctypes - ctypes handles the GIL automatically
>>(release before calling the function, acquire it back after return of
>>the function, and even grabbing the GIL if a callback into Python is
>>done). And it avoids SWIG completely.
[Robin]
> I'm not sure I understand how the last part is done unless all callback
> functions are known to ctypes and even then I'm not sure how ctypes
> would know how to associate a particular call back event with the owner
> thread. Are ctypes callbacks somehow defined uniquely for each callout?
No. In Python 2.2, ctypes creates a new threadtstate for each callback
into Python. In 2.3, the mechanism from PEP 311 is used, which
magically gets the threadstate.
> The other thing I never quite get is what part of the C api I'm allowed
> to use without having the GIL I think it safest to assume I may not call
> any python api without having it.
*Very* few functions, it's mentioned in the docs.
> Wrapping completely independent DLLs seems reasonable, but how does
> ctypes know that a particular extension/dll actually assumes that it has
> the GIL at entry which is mostly what extensions need.
ctypes isn't (or shouldn't be) used to call Python C api functions, only
independent DLLs. (Hm, didn't I post sick examples calling Python's C
api in the past myself <wink>).
Thomas
More information about the Python-list
mailing list