当我运行python脚本时,我可以退出解释器,atexit将执行我注册的所有函数。
现在,我正在使用airflow,并且想要触发atexit tasks on_kill() (即,当我清除或杀死airflow中的dag节点时)。
例如,在伪代码中,我需要能够:
class Foo(PythonOperator):
...
def on_kill():
# somehow, trigger atexit tasks without exiting the
# process entirelyatexit也不是必须的--我可以做其他的事情。主要的一点是,在python上下文之外执行的某些东西需要以过程方式终止,理想情况下,通过引用外壳脚本传递kill函数将是最后的手段(python不会使这种特殊的替代方案变得容易)。
发布于 2019-08-24 09:28:51
你可以像猴子一样修补atexit模块--就像这样:
import atexit
from queue import LifoQueue
save_register = atexit.register
atexit_queue = LifoQueue()
def my_register(func, *args, **kwargs):
save_register(func, *args, **kwargs)
atexit_queue.put((func, args, kwargs))
atexit.register = my_register
if __name__ == '__main__':
def func1():
print('func1() called')
def func2(arg):
print(f'func2({arg}) called')
def func3(arg, kwarg1=1, kwarg2='foo'):
print(f'func3({arg}, kwarg1={kwarg1}, kwarg2={kwarg2!r}) called')
atexit.register(func1)
atexit.register(func2, 1)
atexit.register(func3, 2, kwarg1=42, kwarg2='bar')
print('Calling queued atexit functions:\n')
while atexit_queue.qsize():
func, args, kwargs = atexit_queue.get()
atexit.unregister(func) # Prevent it from being called again.
func(*args, **kwargs)输出:
Calling queued atexit functions:
func3(2, kwarg1=42, kwarg2='bar') called
func2(1) called
func1() called发布于 2020-01-07 20:26:27
如果我没有误解您的问题,您可以通过atexit._run_exitfuncs()触发在atexit中注册的所有函数
import atexit
def do_something():
print('I am doing some work')
def run_on_exit():
print('I run on exit')
def do_something_else():
print('I am doing more work')
if __name__ == '__main__':
atexit.register(run_on_exit)
do_something()
atexit._run_exitfuncs()
do_something_else()输出显示,触发注册的exit函数不会停止程序流,但允许运行更多的函数。
https://stackoverflow.com/questions/57633815
复制相似问题