如何使用交互式线程程序处理stdin、stdout?

2024-04-29 00:10:13 发布

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

我正在尝试使用Python自动化路由器软件升级。我有一个解决方案,作为一个单线程程序工作良好。我想让它多线程。我的问题是,如果设备上没有足够的空间上载软件,它会打印出设备上的文件列表,并提示用户选择要删除的文件。我试图将这段代码置于锁的控制之下,但我的两个线程同时打印出文件列表并提示用户输入。你知道吗

class AsyncIOSupgrade(threading.Thread):

    # Queue of work
    work_queue = None
    # Event that will tell threads to exit
    stopper = None

    def __init__(self, work_queue, stopper):
        super().__init__()
        self.device_queue = work_queue
        self.id = threading.Thread.getName(self)
        self.lock = threading.Lock()
        self.stopper = stopper

    def upgrade_ios(self, ip):
        # Do a bunch of stuff
        with self.lock:
            # build and print out menu of files
            # prompt user for input

输出结果如下:

>>>> Starting upgrade for test-rt-01 on thread Thread-1
>>>> Starting upgrade for test-rt-02 on thread Thread-2
Insufficient space on remote device. Choose file to delete:
0   Don't delete any files
1   c2900-universalk9-mz.SPA.156-3.M0a.bin               106.73MB
2   vdsl.bin.32bdslfw                                    2.56MB
3   c2900-universalk9-mz.SPA.152-4.M5.bin                96.78MB    Insufficient space on remote device. Choose file to delete:
4   c3750e-universalk9-tar.150-2.SE7.tar                 25.08MB
5   credential.lic                                       0.0MB
6   2015-test-T2R2.txt                                   0.02MB
7   T1R2-withBB-2015.txt                            0.02MB
8   pingTest.tcl                                         0.0MB
9   pingTest__09-25-2015_01.54.33.txt                    0.0MB
10   pingTest__09-25-2015_02.19.50.txt                    0.0MB

Please choose an option: 0   Don't delete any files
1   c2900-universalk9-mz.SPA.156-3.M0a.bin               106.73MB
2   vdsl.bin.32bdslfw                                    2.56MB
3   credential.lic                                       0.0MB
4   c2900-universalk9-mz.SPA.152-4.M5.bin                96.78MB
5   c3750e-universalk9-tar.150-2.SE7.tar                 25.08MB
6   test.cfg                                             0.0MB
7   2015-test-T2R1.txt                                   0.02MB
8   T1R1-withBB-2015.txt                            0.02MB
9   music-on-hold-branch.au                              1.13MB
10   vstack                                               0.0MB
11   smartinstall_db.rev2                                 0.0MB
12   log.txt                                              0.0MB

难道一个线程不应该拥有锁并打印并提示输入,而另一个线程则在等待锁吗?你知道吗


Tags: 文件testselftxtbinqueueonmb