如何识别运行main()的进程,并在signal_handler运行之前终止该进程?
import signal
import sys
import logging
import time
import os
def main():
print("Main function execution")
print("Waiting")
signal.signal(signal.SIGHUP, signal_handler)
time.sleep(10)
#How to stop process before signal handler is called?
def signal_handler(sig, frame):
logging.basicConfig(filename="DelFIle.log", format='%(asctime)s %(message)s',filemode='w')
logging.warning(os.getcwd()+"/Del_folder")
os.remove(os.path.join(os.getcwd(),"/Del_folder/index.txt"))
def Function2():
common resource for main and signal_handler
if __name__=="__main__":
main()
由于主函数可能包含信号处理程序也可能正在使用的资源,因此由于公共资源处理,我收到了输入/输出错误
1条答案
按热度按时间gudnpqoy1#
在signal_handler的功能中添加以下代码帮助我创建了另一个进程来访问FUnction2,而无需争用资源