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

Skip to content

Commit 1a67bee

Browse files
committed
Add a "skull and crossbones" to Py_AddPendingCall.
1 parent 7ab8c87 commit 1a67bee

1 file changed

Lines changed: 30 additions & 25 deletions

File tree

Doc/c-api/init.rst

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ pointer.
442442
standard :mod:`zlib` and :mod:`hashlib` modules release the GIL when
443443
compressing or hashing data.
444444
445+
446+
.. _gilstate:
447+
445448
Non-Python created threads
446449
--------------------------
447450
@@ -905,41 +908,43 @@ Asynchronous Notifications
905908
906909
A mechanism is provided to make asynchronous notifications to the main
907910
interpreter thread. These notifications take the form of a function
908-
pointer and a void argument.
911+
pointer and a void pointer argument.
909912
910-
.. index:: single: setcheckinterval() (in module sys)
911-
912-
Every check interval, when the global interpreter lock is released and
913-
reacquired, Python will also call any such provided functions. This can be used
914-
for example by asynchronous IO handlers. The notification can be scheduled from
915-
a worker thread and the actual call than made at the earliest convenience by the
916-
main thread where it has possession of the global interpreter lock and can
917-
perform any Python API calls.
918913
919914
.. c:function:: int Py_AddPendingCall(int (*func)(void *), void *arg)
920915
921916
.. index:: single: Py_AddPendingCall()
922917
923-
Post a notification to the Python main thread. If successful, *func* will be
924-
called with the argument *arg* at the earliest convenience. *func* will be
925-
called having the global interpreter lock held and can thus use the full
926-
Python API and can take any action such as setting object attributes to
927-
signal IO completion. It must return 0 on success, or -1 signalling an
928-
exception. The notification function won't be interrupted to perform another
929-
asynchronous notification recursively, but it can still be interrupted to
930-
switch threads if the global interpreter lock is released, for example, if it
931-
calls back into Python code.
918+
Schedule a function to be called from the main interpreter thread. On
919+
success, 0 is returned and *func* is queued for being called in the
920+
main thread. On failure, -1 is returned without setting any exception.
932921
933-
This function returns 0 on success in which case the notification has been
934-
scheduled. Otherwise, for example if the notification buffer is full, it
935-
returns -1 without setting any exception.
922+
When successfully queued, *func* will be *eventually* called from the
923+
main interpreter thread with the argument *arg*. It will be called
924+
asynchronously with respect to normally running Python code, but with
925+
both these conditions met:
936926
937-
This function can be called on any thread, be it a Python thread or some
938-
other system thread. If it is a Python thread, it doesn't matter if it holds
939-
the global interpreter lock or not.
927+
* on a :term:`bytecode` boundary;
928+
* with the main thread holding the :term:`global interpreter lock`
929+
(*func* can therefore use the full C API).
940930
941-
.. versionadded:: 3.1
931+
*func* must return 0 on success, or -1 on failure with an exception
932+
set. *func* won't be interrupted to perform another asynchronous
933+
notification recursively, but it can still be interrupted to switch
934+
threads if the global interpreter lock is released.
942935
936+
This function doesn't need a current thread state to run, and it doesn't
937+
need the global interpreter lock.
938+
939+
.. warning::
940+
This is a low-level function, only useful for very special cases.
941+
There is no guarantee that *func* will be called as quick as
942+
possible. If the main thread is busy executing a system call,
943+
*func* won't be called before the system call returns. This
944+
function is generally **not** suitable for calling Python code from
945+
arbitrary C threads. Instead, use the :ref:`PyGILState API<gilstate>`.
946+
947+
.. versionadded:: 3.1
943948
944949
.. _profiling:
945950

0 commit comments

Comments
 (0)