def kbhit():
    dr, dw, de = select([sys.stdin], [], [], delay)
return dr != []

def check_mail():
    print('Checking mail')
if __name__ == '__main__':
    atexit.register(set_normal_term)
set_curses_term()
while 1:
    if kbhit():
        ch = getch()
break
check_mail()
print('done')
Continous loop and exiting in python