Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在dtor或python上连接线程_Python_Multithreading_Python Multithreading - Fatal编程技术网

在dtor或python上连接线程

在dtor或python上连接线程,python,multithreading,python-multithreading,Python,Multithreading,Python Multithreading,我创建了一个类,该类在ctor中启动新线程(\uuuu init\uuuu) 并在dtor中连接线程(\uuuu del\uuu) 像这样: import threading import time class Dthread: m_thread: threading.Thread def __init__(self, interval=1): self.interval = interval self.m_thread = threadin

我创建了一个类,该类在ctor中启动新线程(
\uuuu init\uuuu
) 并在dtor中连接线程(
\uuuu del\uuu
) 像这样:

import threading
import time


class Dthread:
    m_thread: threading.Thread

    def __init__(self, interval=1):
        self.interval = interval
        self.m_thread = threading.Thread(target=self.run, args=())
        self.m_thread.daemon = True
        self.m_thread.start()

    def __del__(self):
        self.m_thread.join()

    def run(self):
        while True:
            print('Doing something important in the background')
            time.sleep(self.interval)


g = Dthread(True)
print('Checkpoint')
print('Bye')

在“main function”启动后,我希望运行线程继续运行,但在IDE中运行此代码时,我得到:

Doing something important in the background 
Checkpoint
Bye 
当我把它放进去的时候

我得到:

Doing something important in the background 
Checkpoint
Bye
Doing something important in the background 
Doing something important in the background 
Doing something important in the background ... 

如果从命令行运行,程序将按预期运行。所以,我猜您运行的是IDE或jupyter笔记本或类似的东西,其中没有真正的退出处理,因此守护进程线程被终止。我已经修改了程序,使其具有一个单独的
Runner
类,该类将包含重要的代码。它将检查
stop
条件,并在其
run
方法中终止其循环,以便线程可以实际连接(原始代码中的
join
调用实际上对于守护进程线程来说是多余的,当剩下非守护进程线程时,守护进程线程会自动终止,并且无论如何都不会完成,因为
run
方法永远不会返回)

这个
Runner
类需要与Dthread类分开,Dthread类的析构函数调用
Runner
类的
stop
方法,然后加入线程。这是因为我发现,当线程在当前运行析构函数的同一实例上运行方法时,它不起作用。请注意通过将实例设置为
None
并强制垃圾收集,确保调用
Dthread
实例的析构函数。当然,您可以直接调用
Dthread
实例上的
join
方法

import threading
import time
import gc

class Dthread:
    m_thread: threading.Thread

    class Runner():
        def __init__(self, interval):
            self._interval = interval
            self._stop = False

        def run(self):
            while not self._stop:
                print('Doing something important in the background')
                time.sleep(self._interval)

        def stop(self):
            self._stop = True



    def __init__(self, interval=1):
        self.stop = False
        self.joined = False
        self.runner = Dthread.Runner(interval)
        self.m_thread = threading.Thread(target=self.runner.run, args=())
        #self.m_thread.daemon = True
        self.m_thread.start()

    def __del__(self):
        print('dtor called.')
        self.join()

    def join(self):
        if not self.joined:
            self.runner.stop()
            self.m_thread.join()
            self.joined = True


g = Dthread(1.0)
print('Checkpoint')
print('Bye')
g = None # or g.join()
gc.collect()

我试着添加del,但仍然不起作用