5
5
Seems to happen with very large history being loaded and causing slowdowns.
6
6
7
7
"""
8
+ import re
8
9
import time
9
10
10
11
from prompt_toolkit import PromptSession
11
12
from prompt_toolkit .history import History , ThreadedHistory
12
13
13
- import re
14
-
15
14
16
15
class MegaHistory (History ):
17
16
"""
@@ -20,36 +19,40 @@ class MegaHistory(History):
20
19
Sample designed to exercise existing multitasking hazards, don't add any more.
21
20
"""
22
21
23
- def __init__ (self , init_request :int = 1000 , * args , ** kwargs ):
22
+ def __init__ (self , init_request : int = 1000 , * args , ** kwargs ):
24
23
super (MegaHistory , self ).__init__ (* args , ** kwargs )
25
- self .requested_count = 0 # only modified by main (requesting) thread
26
- self .requested_commands = 0 # only modified by main (requesting) thread
27
- self .actual_count = 0 # only modified by background thread
24
+ self .requested_count = 0 # only modified by main (requesting) thread
25
+ self .requested_commands = 0 # only modified by main (requesting) thread
26
+ self .actual_count = 0 # only modified by background thread
28
27
29
28
def load_history_strings (self ):
30
29
while True :
31
30
while self .requested_count <= self .actual_count :
32
- time .sleep (0.001 ) # don't busy loop
31
+ time .sleep (0.001 ) # don't busy loop
33
32
34
- print (f'... starting to load { self .requested_count - self .actual_count :15,d} more items.' )
33
+ print (
34
+ f"... starting to load { self .requested_count - self .actual_count :15,d} more items."
35
+ )
35
36
while self .requested_count > self .actual_count :
36
37
yield f"History item { self .actual_count :15,d} , command number { self .requested_commands } "
37
38
self .actual_count += 1
38
- print (' ...done.' )
39
+ print (" ...done." )
39
40
40
41
def store_string (self , string ):
41
42
pass # Don't store strings.
42
43
43
44
# called by main thread, watch out for multitasking hazards.
44
- def add_request (self , requested :int = 0 ):
45
+ def add_request (self , requested : int = 0 ):
45
46
self .requested_count += requested
46
47
self .requested_commands += 1
47
48
48
49
def show_request (self ):
49
- print (f'Have loaded { self .actual_count :15,d} of { self .requested_count :15,d} in { self .requested_commands } commands.' )
50
+ print (
51
+ f"Have loaded { self .actual_count :15,d} of { self .requested_count :15,d} in { self .requested_commands } commands."
52
+ )
50
53
51
54
52
- HIST_CMD = re .compile (r' ^hist (load (\d+)|show)$' , re .IGNORECASE )
55
+ HIST_CMD = re .compile (r" ^hist (load (\d+)|show)$" , re .IGNORECASE )
53
56
54
57
55
58
def main ():
@@ -73,17 +76,17 @@ def main():
73
76
74
77
while True :
75
78
text = session .prompt ("Say something: " )
76
- if text .startswith (' hist' ):
79
+ if text .startswith (" hist" ):
77
80
m = HIST_CMD .match (text )
78
81
if not m :
79
- print (' eh?' )
82
+ print (" eh?" )
80
83
else :
81
- if m [1 ] == ' show' :
84
+ if m [1 ] == " show" :
82
85
mh .show_request ()
83
- elif m [1 ].startswith (' load' ):
86
+ elif m [1 ].startswith (" load" ):
84
87
mh .add_request (int (m [2 ]))
85
88
else :
86
- print (' eh? hist load nnnnnn\n or hist show' )
89
+ print (" eh? hist load nnnnnn\n or hist show" )
87
90
pass
88
91
else :
89
92
print ("You said: %s" % text )
0 commit comments