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

在Python线程中,如何跟踪线程的完成情况?

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

在Python线程中,如何跟踪线程的完成情况?

这是@detly答案的一种变体,它使您可以从主线程中指定消息,而不是从目标函数中打印消息。这将创建一个包装函数,该函数调用您的目标,然后在终止之前打印一条消息。您可以将其修改为在每个线程完成之后执行任何类型的标准清除。

#!/usr/bin/pythonimport threadingimport timedef target1():    time.sleep(0.1)    print "target1 running"    time.sleep(4)def target2():    time.sleep(0.1)    print "target2 running"    time.sleep(2)def launch_thread_with_message(target, message, args=[], kwargs={}):    def target_with_msg(*args, **kwargs):        target(*args, **kwargs)        print message    thread = threading.Thread(target=target_with_msg, args=args, kwargs=kwargs)    thread.start()    return threadif __name__ == '__main__':    thread1 = launch_thread_with_message(target1, "finished target1")    thread2 = launch_thread_with_message(target2, "finished target2")    print "main: launched all threads"    thread1.join()    thread2.join()    print "main: finished all threads"


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

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

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