【问题标题】:Set Daemon Of Python Thread In Class To True将类中 Python 线程的守护进程设置为 True
【发布时间】:2021-10-23 21:37:17
【问题描述】:

我正在编写一个 Texteditor 类:

from threading import *


class Editor(Thread):
      {python code}

if __name__ == "__main__":
      editor = Editor()
      editor.start()

我希望这个编辑器线程作为守护进程运行。我怎样才能做到这一点? 我试过了:

editor = Editor(daemon=True)

editor.daemon = True

self.daemon = True

您好, 戴夫

【问题讨论】:

    标签: python multithreading daemon


    【解决方案1】:

    喜欢下面的

    from threading import *
    
    
    class Editor(Thread):
        def __init__(self, is_daemon: bool):
            super(Editor, self).__init__(daemon=is_daemon)
    
        def run(self):
            print('in run')
    
    
    if __name__ == "__main__":
        editor = Editor(True)
        print(editor.daemon)
        editor.start()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      相关资源
      最近更新 更多