def _set_logfile_close_signal_handler(logfile):
 def _on_signal_close_logfile_before_exit(unused_signal_no, unused_frame):
  logfile.close()
  os.exit(1)
 signal.signal(signal.SIGTERM, _on_signal_close_logfile_before_exit)

def _set_logfile_close_signal_handler ( logfile ) : def _on_signal_close_logfile_before_exit ( unused_signal_no , unused_frame ) : logfile . close ( ) os . exit ( 1 ) signal . signal ( signal . SIGTERM , _on_signal_close_logfile_before_exit )
setup a signal handler to explicitly call logfile .