You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在prompt-toolkit键绑定中使用内部prompt?

解决prompt_toolkit嵌套输入的事件循环冲突问题

核心问题是同步prompt()方法内部会调用asyncio.run()启动新事件循环,和外层运行中的循环冲突。要解决这个问题,必须统一使用异步API,在同一个事件循环里处理所有操作,同时用合适的组件避免终端输入区域被污染。

解决方案步骤

  • 切换到异步架构:将整个应用的入口改为异步,用asyncio.run()启动主逻辑,外层使用prompt_async()而非同步prompt()。
  • 异步键绑定处理函数:把按键触发的处理函数定义为异步函数,内部调用异步输入方法(或弹窗组件)获取用户输入,避免启动新事件循环。
  • 使用弹窗组件隔离输入:用prompt_toolkit的InputDialog组件获取内部输入,不会占用主输入行,避免终端被命令填满,同时保留即时按键响应特性。

完整示例代码

import asyncio
from prompt_toolkit import PromptSession
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.widgets import InputDialog
from prompt_toolkit.bottom_toolbar import BottomToolbar

# 定义底部工具栏
bottom_toolbar = BottomToolbar(text="Press Ctrl+A to trigger input, Ctrl+Q to quit")

# 创建按键绑定
kb = KeyBindings()

async def get_user_input():
    # 用弹窗获取输入,不污染主输入区域
    dialog = InputDialog(
        title="Input Required",
        text="Enter your content here:",
    )
    result = await dialog.run_async()
    if result:
        print(f"\nReceived input: {result}")  # 替换为你的业务处理逻辑

@kb.add('c-a')
async def _(event):
    # 触发输入弹窗
    await get_user_input()
    # 恢复主会话的输入焦点
    event.app.layout.focus(event.app.current_buffer)

@kb.add('c-q')
def _(event):
    event.app.exit()

async def main():
    # 创建异步PromptSession
    session = PromptSession(
        bottom_toolbar=bottom_toolbar,
        key_bindings=kb,
        prompt="> "
    )
    while True:
        try:
            # 异步获取主输入(不需要主输入可替换为事件等待逻辑)
            text = await session.prompt_async()
            print(f"Main input processed: {text}")
        except KeyboardInterrupt:
            continue
        except EOFError:
            break

if __name__ == "__main__":
    asyncio.run(main())

代码说明

  • 异步统一循环:主函数main()为异步,由asyncio.run()启动,所有操作在同一个事件循环内执行,彻底避免循环冲突。
  • 即时按键响应:按键绑定无需回车即可触发,保留原有交互逻辑,底部工具栏持续显示状态提示。
  • 输入隔离:InputDialog作为独立弹窗获取输入,完成后自动关闭,不会在主输入区域留下冗余内容,满足终端整洁的需求。

若不需要主输入功能,可将session.prompt_async()替换为asyncio.Event的等待循环,仅保留底部工具栏和按键响应逻辑。

内容的提问来源于stack exchange,提问作者Nick K9

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 02:35:09