Boost::Python: safely store a Python callable and its arguments to call it later
<p>I want to create Python bindings for a C++ component <strong>invoker</strong> which takes a 
function object and calls it <em>later</em> from another thread. 
Usage in C++ looks like this:</p>

<pre><code>invoker my_invoker(thread_holding_context);

// this will queue ome_class::some_method() to be executed later in another
// thread and it returns immediately
my_invoker.invoke(std::bind(&amp;some_class::some_method, this));
</code></pre>

<p>Of course you can use pure Python code for this rather than wrapping a C++ 
component, but this <code>invoker</code> is part of a framework which is used in a greater
context and I <em>have</em> to use it.</p>

<p>In Boost::Python a wrapper could look like this:</p>

<pre><code>struct invoker_wrap 
    : public invoker, public wrapper&lt;invoker&gt; {
    explicit invoker_wrap(thread_holding_context) {...}

    void wrap_invoke(boost::python::object callable) {
        // queue the callable - don't execute it yet
        invoker::invoke(std::bind(&amp;invoker_wrap::call, this, callable));
    }

    void call(boost::python::object callable) {
        std::cout &lt;&lt; "call: " &lt;&lt; callable.ptr() &lt;&lt; std::endl;
        PyGILState_STATE gstate = PyGILState_Ensure();  // seems to be not needed
        callable();
        PyGILState_Release(gstate);                     // seems to be not needed
    }
};

class_&lt;invoker_wrap, boost::noncopyable&gt;("invoker", init&lt;thread_holding_context &amp;&gt;())
    .def("invoke", &amp;invoker_wrap::wrap_invoke);
</code></pre>

<p>The Python code looks similar to this:</p>

<pre><code>_invoker = invoker(thread_holding_context)

def my_func():
    print("%.1f %s: my_func()" % (time.time(), threading.currentThread().getName()))

def thread_func():
    while True:
        _invoker.invoke(my_func)
        time.sleep(1)

threading.Thread(target=thread_func).start()
</code></pre>

<p>This code somehow works - but it behaves strangely. <code>invoker::call</code> is being
called correctly but the but the output of the callable itself is executed 
<strong>much later</strong> (up to 57 secs to be precise).</p>

<p>I have two questions now: </p>

<ul>
<li><p>since the <code>invoker</code> doesn't run the callable immediately but stores it
a queue, <code>invoke()</code> returns before the call and Python should clean up 
all data needed for the call. Doesn't it? 
Should I <strong>copy</strong> or <strong>protect</strong> the callable and <em>all it's arguments</em>?
How can this be done?</p></li>
<li><p>Why is the output from the Python code printed later while the C++ 
output isn't? Since the C++ code and the Python code run in the same 
process they share <code>stdout</code>. I can solve this  by calling 
<code>sys.stdout.flush()</code> in the callback but I don't understand why.</p></li>
</ul>