2014-11-01 4 views
1

IPython.embed()을 파이썬 스크립트로 호출 할 때 시간 초과가있는 방법이 있습니까? 여러 장소에서 IPython.embed()에 대해 중지하는 스크립트가 있다고 가정 해보십시오. 사용자가 시간에 응답하지 않으면 어떻게 스크립트를 계속 사용할 수 있습니까?IPython을 호출 할 때 시간 초과 embed()

+0

현재 API 그러나 디버깅을 위해 사용할 때 유용 할 수있는 팁은''% kill_embedded'' 매크로입니다 :''IPython.embed()''에서 사용하면 다른 세션을 다시 열지 않을 것입니다. – elias

+0

고마워, 그건 편리한 트릭이긴하지만 내가 찾고있는 것이 아니다. –

+0

입력 타임 아웃 솔루션 ('Type 'i/I')을 사용하여 IPython 모드로 들어간다 : \ n .... 1 \ n .... 2 \ n .... 3 \ n ... .4 \ n .... 5 \ nSorry too late') 여기에 설명 된대로 http://stackoverflow.com/questions/1335507/keyboard-input-with-timeout-in-python –

답변

1

이 질문에 대한 답변은 아니지만 원하는 내용으로는 충분합니다.

나는

는 IPython.embed()를해야하는 경우 사용자가 시간 창 내에서 결정하도록 다음과 같이 호출해야합니다.

import time 
import IPython 

try: 
    from msvcrt import kbhit 
except ImportError: 
    import termios, fcntl, sys, os 
    def kbfunc(): 
     fd = sys.stdin.fileno() 
     oldterm = termios.tcgetattr(fd) 
     newattr = termios.tcgetattr(fd) 
     newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO 
     termios.tcsetattr(fd, termios.TCSANOW, newattr) 
     oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) 
     fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) 
     try: 
      while True: 
       try: 
        c = sys.stdin.read(1) 
        return c.decode() 
       except IOError: 
        return False 
     finally: 
      termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) 
      fcntl.fcntl(fd, fcntl.F_SETFL, oldflags) 
else: 
    import msvcrt 
    def kbfunc(): 
     #this is boolean for whether the keyboard has bene hit 
     x = msvcrt.kbhit() 
     if x: 
      #getch acquires the character encoded in binary ASCII 
      ret = msvcrt.getch() 
      return ret.decode() 
     else: 
      return False 

def wait_for_interrupt(waitstr=None, exitstr=None, countdown=True, delay=3): 
    if waitstr is not None: print(waitstr) 
    for i in range(delay*10): 
     if countdown and i%10 ==0 : print('%d'%(i/10 + 1), end='') 
     elif countdown and (i+1)%10 ==0: print('.') 
     elif countdown     : print('.', end='') 

     key = kbfunc() 
     if key: return key 
     time.sleep(0.1) 
    if exitstr is not None: print(exitstr) 
    return False 

if __name__ == "__main__": 
    #wait_for_interrupt example test 
    if wait_for_interrupt('wait_for_interrupt() Enter something in the next 3 seconds', '... Sorry too late'): 
     IPython.embed() 

    #begin the counter 
    number = 1 

    #interrupt a loop 
    while True: 

     #acquire the keyboard hit if exists 
     x = kbfunc() 

     #if we got a keyboard hit 
     if x != False and x == 'i': 
      #we got the key! 
      IPython.embed() 
      #break loop 
      break 
     else: 
      #prints the number 
      print(number) 
      #increment, there's no ++ in python 
      number += 1 
      #wait half a second 
      time.sleep(0.5) 
+0

흥미로운 솔루션! =) – elias

+0

'c = sys.stdin.read (1)'행 다음에'F2 '와 같은 키가 눌려지면'stdin'에 데이터가 남아있어 트리거가 실행되는 동안 오류가 발생했습니다 다음의'wait_for_interrupt()'. 의도하지 않은 버그를 도입하지 않고이를 해결하는 방법은 무엇입니까? –

+0

그리고'wait_for_interrupt()'가 호출되기 전에'stdin'이 이미 데이터를 가지고있는 경우는 어떨까요? 'stdin'을 가정하거나'wait_for_interrupt()'를'context_manager' ('with' 문)와 함께 사용해서 자체적으로 /'stdin 다음에 준비하고 클린업해야한다. '? –

관련 문제