如何从另一个线程上调用的回调返回主线程 [英] How to return to the main thread from a callback called on another thread

查看:359
本文介绍了如何从另一个线程上调用的回调返回主线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从后台回调执行代码到主线程.由于某些原因,我没有找到执行该操作的简单示例.

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.

我发现了带有队列和池的示例,这显然不是我想要的.我只想加入主线程以执行我的功能.

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.

这是一些简单的代码:

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.

**编辑**

这是我的问题: 主文件:

Here is my problem: main file:

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)

从另一个文件中,我有一个装饰器来处理套接字事件:

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

@sio.on('test')
def test(sid, data):
    print threading.currentThread()
    sio.emit('event triggered')

这是问题所在:我有一些由硬件按钮触发的事件,这些事件触发了调用sio"test"事件的回调.但是因为事件未触发到同一线程中,所以给我带来了一些麻烦:

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:

# async call !
def event_cb(num):
    sio.emit('test')

GPIO.add_event_detect(btNumber, GPIO.RISING, event_cb)

测试方法装饰器称为: 但是当在主线程上未完成发射操作时,此操作将停止.

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

只要从 Mainthread 完成套接字调用,就可以. 但是当在 DummyThread 上进行一次调用时,此操作不再起作用.

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

我在具有socket.io实现的客户端设备上进行测试.只要"emit"在主线程上完成,它就可以工作,但是当我在另一个线程上执行时(例如使用触发回调的按钮,它就会停止工作)

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)

这就是为什么我想表演

推荐答案

您可以使用Queue实例轻松地在线程之间传递值.

You can easily pass values between threads by using a Queue instance.

这个简单的演示脚本解释了这个概念.您可以使用 Ctrl - C 中止脚本.

This simple demonstration script explains the concept. You can abort the script using Ctrl-C.

#!/usr/bin/env python
import Queue
import random
import threading
import time

def main_thread():
    """Our main loop in the main thread.

    It receives the values via a Queue instance which it passes on to the
    other threads on thread start-up.
    """
    queue = Queue.Queue()

    thread = threading.Thread(target=run_in_other_thread,
                              args=(queue,))
    thread.daemon = True  # so you can quit the demo program easily :)
    thread.start()

    while True:
         val = queue.get()
         print "from main-thread", val

def run_in_other_thread(queue):
    """Our worker thread.

    It passes it's generated values on the the main-thread by just
    putting them into the `Queue` instance it got on start-up.
    """
    while True:
         queue.put(random.random())
         time.sleep(1)

if __name__ == '__main__':
    main_thread()

这篇关于如何从另一个线程上调用的回调返回主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆