我在慶典使用蟒蛇2.5.2如何防止shell在運行python腳本時被輸入(鍵盤)?
運行一個腳本,腳本轉儲在外殼上的一些報道。
我怎樣才能凍結外殼,並防止它被被打斷回車鍵(或任何其他鍵)運行該腳本時?
我在慶典使用蟒蛇2.5.2如何防止shell在運行python腳本時被輸入(鍵盤)?
運行一個腳本,腳本轉儲在外殼上的一些報道。
我怎樣才能凍結外殼,並防止它被被打斷回車鍵(或任何其他鍵)運行該腳本時?
實現這一目標的更復雜的方式是在一個線程中運行它,並將輸出寫入文件。
這是如何在一個線程中實現這一點的。
import time
from threading import Thread
def noInterrupt():
f = open('/path/to/filename.txt','w+')
for i in xrange(4):
f.write(i)
time.sleep(1)
f.close()
a = Thread(target=noInterrupt)
a.start()
a.join()
print "done"
如果特定用例要求您獲得鎖的代碼如下從here refrenced:
class KeyboardLocker:
def __init__(self, serio=0):
self._on = False
self.serio = serio
def on(self):
return self._on
def write_value(self,path, value):
with open(path, "a") as f:
f.write(value)
def toggle(self):
if self.on():
self.turn_off()
else:
self.turn_on()
def description(self):
path = '/sys/devices/platform/i8042/serio%d/description' % (self.serio,)
with open(path, "r") as f:
description = f.read()
return description
def turn_on(self):
try:
self.write_value('/sys/devices/platform/i8042/serio%d/bind_mode' % (self.serio,),
'auto')
except IOError, e:
self._on = False
raise
else:
self._on = True
return self.on()
def turn_off(self):
try:
self.write_value('/sys/devices/platform/i8042/serio%d/bind_mode' % (self.serio,),
'manual')
self.write_value('/sys/devices/platform/i8042/serio%d/drvctl' % (self.serio,),
'psmouse')
except IOError, e:
self._on = True
raise
else:
self._on = False
return self.on()
if __name__ == "__main__":
kl = KeyboardLocker(serio=0)
device = kl.description()
print "We got a lock on", device
proceed = raw_input("Do you want to proceed? (y/n)").lower().startswith("y")
import sys
if not proceed: sys.exit(1)
kl.turn_off()
import time
wait = 5
print "Sleeping few seconds...", wait
time.sleep(wait)
print "Voila!"
kl.turn_on()
raw_input("Does it work now?")
如果我理解正確的話,你要禁用的用戶鍵盤輸入的回聲?
你可以用下面的命令禁用鍵盤迴音:
stty -echo
,並再次啓用它:
stty echo
(但是你wan't能夠看到你所輸入的內容)