avoid use of time.sleep() in event loop

This commit is contained in:
The MMGen Project 2022-05-26 16:07:19 +00:00
commit 4f260056e8
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 15 additions and 33 deletions

View file

@ -82,7 +82,7 @@ class MMGenTermLinux(MMGenTerm):
break
@classmethod
def get_char(cls,prompt='',immed_chars='',prehold_protect=True,num_chars=5,sleep=None):
def get_char(cls,prompt='',immed_chars='',prehold_protect=True,num_chars=5):
"""
Use os.read(), not file.read(), to get a variable number of bytes without blocking.
Request 5 bytes to cover escape sequences generated by F1, F2, .. Fn keys (5 bytes)
@ -90,8 +90,6 @@ class MMGenTermLinux(MMGenTerm):
"""
timeout = 0.3
tty.setcbreak(cls.stdin_fd)
if sleep:
time.sleep(sleep)
msg_r(prompt)
if g.test_suite:
prehold_protect = False
@ -111,10 +109,8 @@ class MMGenTermLinux(MMGenTerm):
return s
@classmethod
def get_char_raw(cls,prompt='',num_chars=5,sleep=None):
def get_char_raw(cls,prompt='',num_chars=5):
tty.setcbreak(cls.stdin_fd)
if sleep:
time.sleep(sleep)
msg_r(prompt)
s = os.read(cls.stdin_fd,num_chars).decode()
termios.tcsetattr(cls.stdin_fd, termios.TCSADRAIN, cls.old_term)
@ -127,9 +123,7 @@ class MMGenTermLinuxStub(MMGenTermLinux):
cls.stdin_fd = sys.stdin.fileno()
@classmethod
def get_char(cls,prompt='',immed_chars='',prehold_protect=None,num_chars=None,sleep=None):
if sleep:
time.sleep(0.1)
def get_char(cls,prompt='',immed_chars='',prehold_protect=None,num_chars=None):
msg_r(prompt)
return sys.stdin.read(1)
@ -177,14 +171,12 @@ class MMGenTermMSWin(MMGenTerm):
return
@classmethod
def get_char(cls,prompt='',immed_chars='',prehold_protect=True,num_chars=None,sleep=None):
def get_char(cls,prompt='',immed_chars='',prehold_protect=True,num_chars=None):
"""
always return a single character, ignore num_chars
first character of 2-character sequence returned by F1-F12 keys is discarded
prehold_protect is ignored
"""
if sleep:
time.sleep(sleep)
msg_r(prompt)
timeout = 0.5
while True:
@ -202,14 +194,12 @@ class MMGenTermMSWin(MMGenTerm):
return ch
@classmethod
def get_char_raw(cls,prompt='',num_chars=None,sleep=None):
def get_char_raw(cls,prompt='',num_chars=None):
"""
always return a single character, ignore num_chars
first character of 2-character sequence returned by F1-F12 keys is discarded
"""
while True:
if sleep:
time.sleep(sleep)
msg_r(prompt)
ch = chr(msvcrt.getch()[0])
if ch in '\x00\xe0': # first char of 2-char sequence for F1-F12 keys
@ -221,9 +211,7 @@ class MMGenTermMSWin(MMGenTerm):
class MMGenTermMSWinStub(MMGenTermMSWin):
@classmethod
def get_char(cls,prompt='',immed_chars='',prehold_protect=None,num_chars=None,sleep=None):
if sleep:
time.sleep(0.1)
def get_char(cls,prompt='',immed_chars='',prehold_protect=None,num_chars=None):
msg_r(prompt)
return os.read(0,1).decode()

View file

@ -20,7 +20,7 @@
tw: Tracking wallet dependency classes and helper functions
"""
import sys,time
import sys,time,asyncio
from ..globalvars import g
from ..objmethods import Hilite,InitErrors,MMGenObject
@ -243,7 +243,7 @@ class TwCommon:
self.oneshot_msg = '' if self.oneshot_msg else None # tristate, saves previous state
if reply not in self.key_mappings:
msg_r('\ninvalid keypress ')
time.sleep(0.5)
await asyncio.sleep(0.3)
continue
action = self.key_mappings[reply]

View file

@ -12,6 +12,7 @@
wallet.dieroll: dieroll wallet class
"""
import time
from ..globalvars import g
from ..opts import opt
from ..util import msg,msg_r,die,fmt,block_format,remove_whitespace,keypress_confirm
@ -92,9 +93,9 @@ class wallet(wallet):
from ..term import get_char
def get_digit(n):
p = prompt_fs
sleep = g.short_disp_timeout
while True:
ch = get_char(p.format(n),num_chars=1,sleep=sleep)
time.sleep(g.short_disp_timeout)
ch = get_char(p.format(n),num_chars=1)
if ch in bc.digits:
msg_r(CUR_HIDE + ' OK')
return ch

View file

@ -68,7 +68,7 @@ def tt_line_input():
reply = line_input('\nEnter text: ')
confirm(f'Did you enter the text {reply!r}?')
def tt_get_char(raw=False,one_char=False,sleep=0,immed_chars=''):
def tt_get_char(raw=False,one_char=False,immed_chars=''):
funcname = ('get_char','get_char_raw')[raw]
fs = fmt("""
Press some keys in quick succession.
@ -81,14 +81,10 @@ def tt_get_char(raw=False,one_char=False,sleep=0,immed_chars=''):
'Your entry should be repeated back to you immediately.'
)[raw]
m2 = (
'',
f'\nA delay of {sleep} seconds will added before each prompt'
)[bool(sleep)]
m3 = (
'',
f'\nThe characters {immed_chars!r} will be repeated immediately, the others with delay.'
)[bool(immed_chars)]
m4 = 'The F1-F12 keys will be ' + (
m3 = 'The F1-F12 keys will be ' + (
'blocked entirely.'
if one_char and not raw else
"echoed AS A SINGLE character '\\x1b'."
@ -96,12 +92,10 @@ def tt_get_char(raw=False,one_char=False,sleep=0,immed_chars=''):
'echoed as a FULL CONTROL SEQUENCE.'
)
if g.platform == 'win':
m4 = 'The Escape and F1-F12 keys will be returned as single characters.'
m3 = 'The Escape and F1-F12 keys will be returned as single characters.'
kwargs = {}
if one_char:
kwargs.update({'num_chars':1})
if sleep:
kwargs.update({'sleep':sleep})
if immed_chars:
kwargs.update({'immed_chars':immed_chars})
@ -109,7 +103,7 @@ def tt_get_char(raw=False,one_char=False,sleep=0,immed_chars=''):
funcname,
','.join(f'{a}={b!r}' for a,b in kwargs.items())
))
msg(fs.format( m1, yellow(m2), yellow(m3), yellow(m4) ))
msg(fs.format( m1, yellow(m2), yellow(m3) ))
try:
while True:
@ -159,7 +153,6 @@ tt_urand()
tt_txview()
tt_get_char(one_char=True)
tt_get_char(one_char=True,sleep=1)
tt_get_char(one_char=True,raw=True)
if g.platform == 'linux':