Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6ffb4b

Browse files
committed
document that a new Python thread context is created in ctypes callbacks (closes #6627)
Patch by Nikolaus Rath.
1 parent e83ed43 commit f6ffb4b

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

Doc/library/ctypes.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,12 +1022,18 @@ As we can easily check, our array is sorted now::
10221022
1 5 7 33 99
10231023
>>>
10241024

1025-
**Important note for callback functions:**
1025+
**Important notes for callback functions:**
10261026

10271027
Make sure you keep references to :func:`CFUNCTYPE` objects as long as they are
10281028
used from C code. :mod:`ctypes` doesn't, and if you don't, they may be garbage
10291029
collected, crashing your program when a callback is made.
10301030

1031+
Also, note that if the callback function is called in a thread created outside
1032+
of Python's control (e.g. by the foreign code that calls the callback), ctypes
1033+
creates a new dummy Python thread on every invocation. This behavior is correct
1034+
for most purposes, but it means that values stored with `threading.local` will
1035+
*not* survive across different callbacks, even when those calls are made from
1036+
the same C thread.
10311037

10321038
.. _ctypes-accessing-values-exported-from-dlls:
10331039

0 commit comments

Comments
 (0)