2017-01-09 55 views
0

取数据我是很新,诅咒一个noob问题很抱歉:)我跑while True:循环,从API获取数据,并使用功能show_header_and_footer()呈现出来。然后它睡3秒钟以避免不断刷新并超过API提供程序限制。Python的诅咒:从API和残培()在同一时间

if __name__ == "__main__": 

    setup_curses() 

    while True: 
     catch_input() 
     show_header_and_footer() 
     stdscr.refresh() 
     header.refresh() 
     footer.refresh() 
     time.sleep(3) 

正如你可能已经注意到,也有对catch_input()函数的调用看起来像:

def catch_input(): 
    c = stdscr.getch() 
    if c in (ord('q'), ord('Q')): 
     curses.raw() 
     curses.endwin() 

一切正常,但点击“Q”后,我要等到time.sleep(3)结束。我该如何改进?

回答

1

注意:我假设按'q'or'Q',你退出应用程序。

如果这是真的,为什么不从catch_input()方法返回一个状态到main。使用此状态从while(True)循环返回/中断。这样你就不会遇到3秒钟的计时器。

catch_Input()方法:

def catch_input(): 
    c = stdscr.getch() 
    if c in (ord('q'), ord('Q')): 
     curses.raw() 
     curses.endwin() 
     return False 
    return True 

Main方法():

if __name__ == "__main__": 

setup_curses() 

while True: 
    if not catch_input(): 
     break 
    show_header_and_footer() 
    stdscr.refresh() 
    header.refresh() 
    footer.refresh() 
    time.sleep(3) 
+0

它不工作,什么都没有改变 – mrpapa