prompt-toolkit / prompt-toolkit/python-prompt-toolkit

How do I fix Exception This event loop is already running with KeyBindings?

Open
#1,166 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10.6k
Forks
815
PR merge metrics
No merged PRs in 30d

Description

I'll show this issue with a small example and error I get. I'm making a notes CLI app where first the user is presented with a list of notes. They can enter a number (0-9) to go to the specific note. Once they are in the note, there's a timer for 1 second. Once this timer is complete, they will go back to the main page, where they can see all the notes again.

Here's the code:

import os
import time

from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit import prompt

notes = [
    {'t': 'First note', 'c': 'This is the content of the first notes'},
    {'t': 'Second note', 'c': 'This is the content of the second notes'},
    {'t': 'Third note', 'c': 'This is actually the content of the thid notes'}
]

os.system('clear')


def main():
  kb = KeyBindings()
  # control C to exit
  @kb.add('c-c')
  def _(event): exit()

  for i in range(10):
    # dynamically add keybindings 0 to 9 for each note
    @kb.add(f'{i}')
    def _(event):
      note_index = event.key_sequence[0].key
      goto_note(note_index)

  def show_notes():
    # show all notes and listen for key bindings
    for index, note in enumerate(notes):
      print(f'[{index}] {note["t"]}')
    _inp = prompt('> ', key_bindings=kb)

  def goto_note(index):
    # show separate notes
    print(f'going to note {index}')
    # after a second, go back to the main screen
    time.sleep(1)
    show_notes() # <--- error occurs (Exception This event loop is already running)

  show_notes()


if __name__ == '__main__':
  main()

The code works fine until I want to show the main screen after 1 second in the notes page. The error I get is:

Exception This event loop is already running

I believe this is because I am referencing the variable kb inside functions that are decorated by @kb.add.

Is there a way to fix this?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Python example and tracing main(), show_notes(), and goto_note(), especially the nested prompt(..., key_bindings=kb) call. Determine how the event loop is entered and re-entered, then verify that returning to the notes list after the one-second delay works without the exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.