Skip to content

Commit 546b578

Browse files
author
Bob Hyman
committed
Modify ThreadedHistory example(s) for new API
1 parent 1c3056d commit 546b578

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

examples/prompts/history/slow-history.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
By wrapping it in `ThreadedHistory`, the history will load in the background
66
without blocking any user interaction.
77
"""
8+
import asyncio
89
import time
910

1011
from prompt_toolkit import PromptSession
@@ -32,11 +33,22 @@ def main():
3233
"Even when the input is accepted, loading will continue in the "
3334
"background and when the next prompt is displayed.\n"
3435
)
35-
our_history = ThreadedHistory(SlowHistory())
36+
37+
my_loop = asyncio.get_event_loop() # creates loop if needed
38+
39+
# Inform ThreadedHistory which event loop to use
40+
# when passing lines of history to the prompt.
41+
42+
our_history = ThreadedHistory(SlowHistory(), my_loop)
3643

3744
# The history needs to be passed to the `PromptSession`. It can't be passed
3845
# to the `prompt` call because only one history can be used during a
3946
# session.
47+
# Note that PromptSession runs on the thread's current event loop because
48+
# it was created above and is therefore in synch with ThreadedHistory.
49+
# PromptSession would create and event loop if it didn't find one
50+
# already running, but then ThreadedHistory would not work.
51+
4052
session = PromptSession(history=our_history)
4153

4254
while True:

prompt_toolkit/history.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import datetime
1111
import os
1212
import time
13+
import warnings
1314
from abc import ABCMeta, abstractmethod
1415
from threading import Thread
1516
from typing import Callable, Iterable, List, Optional
16-
import warnings
1717

1818
__all__ = [
1919
"History",
@@ -40,7 +40,10 @@ def __init__(self) -> None:
4040
# Methods expected by `Buffer`.
4141
#
4242

43-
def load(self, item_loaded_callback: Callable[[str], None],) -> None:
43+
def load(
44+
self,
45+
item_loaded_callback: Callable[[str], None],
46+
) -> None:
4447
"""
4548
Load the history and call the callback for every entry in the history.
4649
This one assumes the callback is only called from same thread as `Buffer` is using.
@@ -103,13 +106,13 @@ class ThreadedHistory(History):
103106
"""
104107

105108
def __init__(
106-
self, history: History, event_loop: asyncio.BaseEventLoop = None
109+
self, history: History, event_loop: Optional[asyncio.AbstractEventLoop] = None
107110
) -> None:
108111
"""Create instance of ThreadedHistory
109112
110113
Args:
111114
history (History): Instance of History intended to run on a background thread.
112-
event_loop (asyncio.BaseEventLoop, optional): The event loop on which prompt toolkit is running.
115+
event_loop (asyncio.AbstractEventLoop, optional): The event loop on which prompt toolkit is running.
113116
(Deprecated) Defaults to ``asyncio.get_event_loop(), which may *create* the event loop. Caller should provide an explicit value.
114117
"""
115118
self.history = history
@@ -151,7 +154,10 @@ def call_all_callbacks(item: str) -> None:
151154
self._load_thread.daemon = True
152155
self._load_thread.start()
153156

154-
def bg_loader(self, item_loaded_callback: Callable[[str], None],) -> None:
157+
def bg_loader(
158+
self,
159+
item_loaded_callback: Callable[[str], None],
160+
) -> None:
155161
"""
156162
Load the history and schedule the callback for every entry in the history.
157163
TODO: extend the callback so it can take a batch of lines in one event_loop dispatch.

0 commit comments

Comments
 (0)