Graceful forced exit?

Is there a way for an application to signal to ipython that it should exit gracefully? My use case is installing a signal handler to force IPython to exit.

I would like something like this to work:

In [1]: import sys, signal

In [2]: def exit_now(*args): sys.exit()

In [3]: signal.signal(signal.SIGALRM, exit_now)
Out[3]: 0

In [4]: signal.alarm(2)
Out[4]: 0

In [5]:

It used to be that forcing an EOF on stdin would work, but prompt_toolkit does not handle that correctly anymore. See also prompt-toolkit/python-prompt-toolkit#392