栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Python中传递和运行回调方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Python中传递和运行回调方法

除非具有对管理器的引用,否则该线程无法调用管理器。最简单的方法是管理器在实例化时将其提供给线程。

class Manager(object):    def new_thread(self):        return MyThread(parent=self)    def on_thread_finished(self, thread, data):        print thread, dataclass MyThread(Thread):    def __init__(self, parent=None):        self.parent = parent        super(MyThread, self).__init__()    def run(self):        # ...        self.parent and self.parent.on_thread_finished(self, 42)mgr    = Manager()thread = mgr.new_thread()thread.start()

如果您希望能够将任意函数或方法分配为回调,而不是存储对管理器对象的引用,则由于方法包装器等原因,这将带来一些问题。设计回调很困难,因此它需要引用管理器
线程。我做了一段时间,没有提出任何我认为有用或优雅的东西。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/668356.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号