这个线程化微调器类的更好的键盘中断检测

2024-04-27 09:25:59 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我已经在Google代码搜索中搜索到的其他Spinner类中编写了这个类。在

它按预期工作,但我正在寻找更好的方法来处理键盘中断和系统退出异常。有更好的方法吗?在

我的代码是:

#!/usr/bin/env python
import itertools
import sys
import threading

class Spinner(threading.Thread):
    '''Represent a random work indicator, handled in a separate thread'''

    # Spinner glyphs
    glyphs = ('|', '/', '-', '\\', '|', '/', '-')
    # Output string format
    output_format = '%-78s%-2s'
    # Message to output while spin
    spin_message = ''
    # Message to output when done
    done_message = ''
    # Time between spins
    spin_delay = 0.1

    def __init__(self, *args, **kwargs):
        '''Spinner constructor'''
        threading.Thread.__init__(self, *args, **kwargs)
        self.daemon = True
        self.__started = False
        self.__stopped = False
        self.__glyphs = itertools.cycle(iter(self.glyphs))

    def __call__(self, func, *args, **kwargs):
        '''Convenient way to run a routine with a spinner'''
        self.init()
        skipped = False

        try:
            return func(*args, **kwargs)
        except (keyboardInterrupt, SystemExit):
            skipped = True
        finally:
            self.stop(skipped)

    def init(self):
        '''Shows a spinner'''
        self.__started = True
        self.start()

    def run(self):
        '''Spins the spinner while do some task'''
        while not self.__stopped:
            self.spin()

    def spin(self):
        '''Spins the spinner'''
        if not self.__started:
            raise NotStarted('You must call init() first before using spin()')

        if sys.stdin.isatty():
            sys.stdout.write('\r')
            sys.stdout.write(self.output_format % (self.spin_message,
                                                   self.__glyphs.next()))
            sys.stdout.flush()
            time.sleep(self.spin_delay)

    def stop(self, skipped=None):
        '''Stops the spinner'''
        if not self.__started:
            raise NotStarted('You must call init() first before using stop()')

        self.__stopped = True
        self.__started = False

        if sys.stdin.isatty() and not skipped:
            sys.stdout.write('\b%s%s\n' % ('\b' * len(self.done_message),
                                           self.done_message))
            sys.stdout.flush()

class NotStarted(Exception):
    '''Spinner not started exception'''
    pass

if __name__ == '__main__':
    import time

    # Normal example
    spinner1 = Spinner()
    spinner1.spin_message = 'Scanning...'
    spinner1.done_message = 'DONE'
    spinner1.init()
    skipped = False

    try:
        time.sleep(5)
    except (KeyboardInterrupt, SystemExit):
        skipped = True
    finally:
        spinner1.stop(skipped)

    # Callable example
    spinner2 = Spinner()
    spinner2.spin_message = 'Scanning...'
    spinner2.done_message = 'DONE'
    spinner2(time.sleep, 5)

提前谢谢你。在


Tags: selffalsetruemessageinitdefsysnot
1条回答
网友
1楼 · 发布于 2024-04-27 09:25:59

您可能不需要担心捕获^{},因为它是由sys.exit()引起的。您可能希望在程序退出之前捕获它来清理一些资源。在

捕获^{}的另一种方法是注册一个信号处理程序来捕获SIGINT。但是,对于您的示例,使用try..except更有意义,因此您走的是正确的道路。在

一些小建议:

  • 或许可以将__call__方法重命名为start,以便更清楚地表明您正在启动一个作业。在
  • 您可能还希望通过在start方法中而不是在构造函数中附加一个新线程,使Spinner类可重用。在
  • 还要考虑当用户点击当前微调器作业的CTRL-C时会发生什么,下一个作业可以启动,还是应用程序应该退出?在
  • 您还可以将spin_message作为start的第一个参数,将其与即将运行的任务相关联。在

例如,下面是一些人如何使用微调器:

dbproc = MyDatabaseProc()
spinner = Spinner()
spinner.done_message = 'OK'
try:
    spinner.start("Dropping the database", dbproc.drop, "mydb")
    spinner.start("Re-creating the database", dbproc.create, "mydb")
    spinner.start("Inserting data into tables", dbproc.populate)
    ...
except (KeyboardInterrupt, SystemExit):
    # stop the currently executing job
    spinner.stop()
    # do some cleanup if needed..
    dbproc.cleanup()

相关问题 更多 >