python - 线程运行时未调用 atexit
问题描述
我希望依靠 atexit 来停止一个否则会阻塞的线程。
我发现当我在线程运行时调用 sys.exit() 时注册的方法不会被调用。
我发现如果线程没有运行,就会调用该函数。
import os
import atexit
import threading
import sys
class Watcher:
def __init__(self, to_watch, callback):
self.path_to_watch = to_watch
self.stop_flag = False
self._monitor_thread = threading.Thread(target=self.monitor_thread)
self._monitor_thread.start()
atexit.register(self.stop)
def stop(self):
print(f'stopping watcher on {self.path_to_watch}')
self.stop_flag=True
def monitor_thread(self):
while not self.stop_flag:
pass
if __name__ == '__main__':
def my_callback( file, action):
print(file, action)
dw = Watcher('.', my_callback)
sys.exit(0)
这是预期的行为吗?我在 atexit 中没有看到任何关于此的文档。
有没有更好的方法来捕捉主线程正在终止并停止我的线程?
解决方案
如果您可以构造代码以便(理想情况下)主线程只有一种退出方式,那么 threading.Event 是主线程向一个或多个其他线程发出信号表明该退出的好方法。例子:
import threading
import time
import sys
class Watcher:
def __init__(self, done):
self.done = done
self._monitor_thread = threading.Thread(target=self.monitor_thread)
self._monitor_thread.start()
def monitor_thread(self):
sys.stderr.write("Watcher started\n")
while not self.done.is_set():
pass
sys.stderr.write("Watcher thread exiting\n")
if __name__ == '__main__':
done = threading.Event()
Watcher(done)
time.sleep(5)
done.set()
sys.stderr.write("Main thread exiting\n")
推荐阅读
- flutter - 颤振问题 | 未为签署配置版本设置密钥库文件
- postgresql - 我在 docker 上执行我的 postgres 数据库查询时遇到问题
- java - 我真的应该在从 jdk 7 迁移到 jdk 8 时替换 java.security.PrivilegedAction 吗?
- sql - PL/SQL 游标创建失败
- bash - Bash - 从 $PATH 打印文件的完整路径
- python - 我想使用 discord.py 创建一个多页帮助命令
- node.js - 使用用户提示的输入功能
- aws-codebuild - AWS 代码构建。无法加载 SDK 解析器
- reactjs - 在变量更改时将 UseSelector 与渲染反应
- javascript - 作为链接的 div 被图片库所笼罩