【问题标题】:How to receive thread callbacks in Python如何在 Python 中接收线程回调
【发布时间】:2014-11-18 01:17:28
【问题描述】:

这可能以前被问过,但我找不到我要找的东西。我有从主 UI 线程(TKinter)启动的后台线程,我希望它将状态更新发送到 UI。我很欣赏一些代码或伪代码或链接,它们显示了如何在 python 中完成此操作。

伪代码: //test.py

def __init__(self, parent):
#Button
self.submit_button = Button(self,
                text="launch_tasks",
                command=self.launch_tasks).pack()
#label
self.label = Label(master, text="Hello, world!")
self.label.pack()

def launch_tasks(self)
   t = Thread(target=self.process_tasks)
   t.start()

def process_tasks(self):
    cnt = getJobs(self);
    self.label = cnt   # I like to update label here
    for(job in jobs):
      process(job)
      self.label = 'processing' + job # I like to update label 
    ...

【问题讨论】:

    标签: multithreading tkinter python-multithreading


    【解决方案1】:

    用添加了事件路由的消息层补充 Tkinter

    虽然 Tkinter/.mainloop() 是一个自包含的模型-视觉-控制器系统,但您可以在线程安全、非阻塞模式下扩展它的功能,并添加补充 (inter -process / any-to-any ) 你自己的消息层,并为它的 localhost/self 参与者配备基于 Tkinter 的事件路由机制,以与 .mainloop() 集成。

    ################################################ SETUP EVENT-ROUTING Injector
    
    self.aSigFromZMQ = "<<aVirtualEventSignalledFromZMQ_LAYER>>"
    
    self.bind( self.aSigFromZMQ, anEventHANDLER )
    #   |
    #   .bind <<virtual_EventNAME>> altogether with <anEventHANDLER>-call
    
    
    ################################################ Context-fully TRIGGER Injector
    
    self.event_generate( self.aSigFromZMQ, aSigContextDICT )
    #   |
    #   .event_generate( <eventNameId>, **args )  #   triggers <eventNameId>
    #                                             # + passes **args, that allows
    #                                             #          to set <keyword>=<value> pairs for Event-fields,
    #                                             #          that are passed to anEventHANDLER via <Event>-object ...
    

    为了说明和点击链接到正版书籍的作者之一,用于多对多消息传递的最先进、令人难以置信的快速、智能和可扩展的进程间消息传递系统(包括基于此的线程间信号)请参考。 >>> https://stackoverflow.com/a/26559710/3666197


    线程到线程信号(无限回调)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 2022-07-05
      • 1970-01-01
      相关资源
      最近更新 更多