我尝试使用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
当另一个线程等待锁定时,是否一个线程没有锁定并打印并提示输入?