当在 while 语句中执行 input() 时,Jupyter 随机忙(卡住)

Jupyter is busy (stuck) randomly when input() is executed inside while statement

有没有人遇到过在 while 语句中执行 input() 时 Jupyter 很忙(卡住)的问题?这个问题是随机发生在我身上的。有时命令框在单元格旁边提示,有时输入()框从不提示。

这是我的代码的简单版本:

from IPython.display import clear_output

class game_quit(Exception):
    def _render_traceback_(self):
        pass
    
def the_game(player_status = "Offering"):
    while True:
        clear_output()
        print("The Game", flush = True)
        print(f"Status: {player_status}", flush = True)
        if (player_status == "Offering") or (player_status == "Wrong input!"):
            play_offer = input("Would you like to play the game (Y or N)? ")
            if play_offer.upper() == "Y":
                player_status = "Playing"
                play_accepted = True
                clear_output()
                break
            elif play_offer.upper() == "N":
                play_accepted = False
                clear_output()
                break
            else:
                player_status = "Wrong input!"
                clear_output()
                continue
        else:
            player_status = "Playing"
            play_accepted = True
            clear_output()
            break
    while play_accepted:
        the_play(player_status)
    else:
        raise game_quit()

def the_play(player_status):
    while True:
        clear_output()
        print("The Game", flush = True)
        print(f"Status: {player_status}", flush = True)
        pet_offer = input("Do you want to go with a (D)og, a (C)at, or a (P)arakeet? ")
        if pet_offer.upper() == "D":
            player_pet = "Dog"
            clear_output()
            break
        elif pet_offer.upper() == "C":
            player_pet = "Cat"
            clear_output()
            break
        elif pet_offer.upper() == "P":
            player_pet = "Parakeet"
            clear_output()
            break
        else:
            player_status = "Wrong input!"
            clear_output()
            continue
    while pet_offer:
        clear_output()
        print(f"Your companion is a {player_pet}", flush = True)
        play_again = input("Would you like to continue playing the game (Y or N)? ")
        if play_again.upper() == "Y":
            play_continue = True
            clear_output()
            break
        elif play_again.upper() == "N":
            play_continue = False
            clear_output()
            break
        else:
            player_status = "Wrong input!"
            clear_output()
            continue
    if play_continue:
        player_status = "Playing"
        the_game(player_status)
    else:
        raise game_quit()

重现问题的步骤:

  1. 执行代码。
the_game()
  1. 用户想玩游戏。
play_offer = input("Would you like to play the game (Y or N)? ")
  1. 用户选择他的宠物。
pet_offer = input("Do you want to go with a (D)og, a (C)at, or a (P)arakeet? ")
  1. 用户想重玩游戏。
play_again = input("Would you like to continue playing the game (Y or N)? ")
  1. 用户应该期望文本框选择他的宠物。
pet_offer = input("Do you want to go with a (D)og, a (C)at, or a (P)arakeet? ")
  1. 一个
The problem:
The text box is not showing. The running code is stuck there.
  1. B
But sometimes:
The text box is showing, and the user can choose his pet.

到目前为止,我唯一的解决办法是重启内核。有人解决过这类问题吗?

此致,
阿德

有人给了我一个见解。问题是因为 clear_output() 异步的问题。然后我创建了这个函数:

from time import sleep
from IPython.display import clear_output

def refresh_screen():
    clear_output()
    sleep(0.02)

并在我的代码中将所有 clear_output() 替换为 refresh_screen()。问题消失了。稍微延迟一下就解决了问题。