How to return to the main thread from a callback called on another thread
<p>I'd like to execute my code from a background callback into the main thread. For some reasons I didn't found simple examples to perform that.</p>

<p>I found example with Queue and pooling, this is clearly not what I want. I just want to join the main thread in order to execute my function.</p>

<p>Here is some simple code:</p>

<pre><code>def my_callback:
    # this callback is called from a third party and I don't have control on it. 
    # But because of this, the function must_be_executed_on_main_thread is
    # executed on the background.
    must_be_executed_on_main_thread()

def must_be_executed_on_main_thread:
    # must be executed on the main thread.
</code></pre>

<p>** Edit **</p>

<p>Here is my problem:
main file:</p>

<pre><code>if __main__ == '__main__'
    sio = socketio.Server()
    app = Flask(__name__)
    app = socketio.Middleware(sio, app)

    # now the thread is blocking into the server pool
    eventlet.wsgi.server(eventlet.listen('', 7000)), app)
</code></pre>

<p>from another file, I've got a decorator who deal with the socket event:</p>

<pre><code>@sio.on('test')
def test(sid, data):
    print threading.currentThread()
    sio.emit('event triggered')
</code></pre>

<p>And here is the problem: I have some events triggered by an hardware button, which trigger a callback who call sio "test" event. But because the events are not triggered into the same thread, this cause me some trouble:</p>

<pre><code># async call !
def event_cb(num):
    sio.emit('test')

GPIO.add_event_detect(btNumber, GPIO.RISING, event_cb)
</code></pre>

<p>the test method decorator is called:
but when the emit is done not on the main thread, this stop working.</p>

<p>as long as the socket call are done from the <strong>Mainthread</strong>, it is ok. 
but when one call is done on <strong>DummyThread</strong>, this not working anymore.</p>

<p>I test on a client device with socket.io implementation. As long as the "emit" are done on the mainthread, it works, but when I perform on another thread (like with a button who trigger the callback, it stop working)</p>

<p>That's why I'd love to perform </p>