Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29338 > unrolled thread

Using Py_AddPendingCall

Started bycss322 <charles.salvia316@gmail.com>
First post2012-09-16 10:08 -0700
Last post2012-09-17 22:42 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Using Py_AddPendingCall css322 <charles.salvia316@gmail.com> - 2012-09-16 10:08 -0700
    Re: Using Py_AddPendingCall Antoine Pitrou <solipsis@pitrou.net> - 2012-09-17 22:42 +0000

#29338 — Using Py_AddPendingCall

Fromcss322 <charles.salvia316@gmail.com>
Date2012-09-16 10:08 -0700
SubjectUsing Py_AddPendingCall
Message-ID<fe3c43a2-9a77-4db2-8836-e15310a6d078@googlegroups.com>
I have an embedded Python program which runs in a thread in C.

When the Python interpreter switches thread context (yielding control to another thread), I'd like to be notified so I can perform certain necessary operations.

It seems that Py_AddPendingCall is exactly what I'm looking for. However, the API docs are pretty brief on this function, and I'm confused as to how Py_AddPendingCall is supposed to be used. From reading the docs, my understanding is that:

(1) A worker thread calls Py_AddPendingCall and assigns a handler function.
(2) When the Python interpreter runs, it calls the handler function whenever it yields control to another thread
(3) The handler itself is executed in the main interpreter thread, with the GIL acquired

I've googled around for example code showing how to use Py_AddPendingCall, but I can't find anything. My own attempt to use it simply doesn't work. The handler is just never called.

My worker thread code:

const char* PYTHON_CODE = 
"while True:\n"
"   for i in range(0,10): print(i)\n"
"\n";

int handler(void* arg)
{
    printf("Pending Call invoked!\n");
    abort();
}

void worker_thread()
{
    PyGILState_STATE state = PyGILState_Ensure();
    Py_AddPendingCall(&handler, NULL);
    PyRun_SimpleString(PYTHON_CODE);
    PyGILState_Release(state);
}

In this example, worker_thread is invoked in C as a pthread worker thread. This loops infinitely, but the pending call handler is never invoked.

So,obviously I'm not understanding the docs correctly.  How is Py_AddPendingCall is supposed to be used?

[toc] | [next] | [standalone]


#29392

FromAntoine Pitrou <solipsis@pitrou.net>
Date2012-09-17 22:42 +0000
Message-ID<mailman.843.1347921765.27098.python-list@python.org>
In reply to#29338
css322 <charles.salvia316 <at> gmail.com> writes:
> 
> (1) A worker thread calls Py_AddPendingCall and assigns a handler function.
> (2) When the Python interpreter runs, it calls the handler function whenever
it yields control to another thread

Not exactly. As the documentation says: "If successful, func will be called 
with the argument arg *at the earliest convenience*."
This is a deliberately vague wording to stress that the function will not be
called as early as you think. It *should* be called in a timely manner, but not
necessarily as soon as the thread switch happens.

Which begs the question:

> In this example, worker_thread is invoked in C as a pthread worker thread. This
> loops infinitely, but the pending call handler is never invoked.

What is your main Python thread doing? Is it running Python code (*)? Or do you
have a main Python thread at all? The "main Python thread" is the one from which
Py_Initialize() was called.

(*) for example, running one of the following functions:
http://docs.python.org/dev/c-api/veryhigh.html

Regards

Antoine.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web