From 4f260056e8184464e60c5c437a38fb20b3865715 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 26 May 2022 16:07:19 +0000 Subject: [PATCH] avoid use of time.sleep() in event loop --- mmgen/term.py | 24 ++++++------------------ mmgen/tw/common.py | 4 ++-- mmgen/wallet/dieroll.py | 5 +++-- test/misc/term.py | 15 ++++----------- 4 files changed, 15 insertions(+), 33 deletions(-) diff --git a/mmgen/term.py b/mmgen/term.py index f116f193..0ff4e5cb 100755 --- a/mmgen/term.py +++ b/mmgen/term.py @@ -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() diff --git a/mmgen/tw/common.py b/mmgen/tw/common.py index 747f2cff..db8eda6a 100755 --- a/mmgen/tw/common.py +++ b/mmgen/tw/common.py @@ -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] diff --git a/mmgen/wallet/dieroll.py b/mmgen/wallet/dieroll.py index c0e4bca0..597e99cc 100755 --- a/mmgen/wallet/dieroll.py +++ b/mmgen/wallet/dieroll.py @@ -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 diff --git a/test/misc/term.py b/test/misc/term.py index c2772e38..f4106999 100755 --- a/test/misc/term.py +++ b/test/misc/term.py @@ -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':