從pycamera docs我把快速採集和處理的例子,並增加了SIGINT事件處理程序來捕獲鍵盤中斷:蟒蛇picamera,鍵盤上的Ctrl + C/SIGINT沒有抓到
import io
import time
import threading
import picamera
# Create a pool of image processors
done = False
lock = threading.Lock()
pool = []
def signal_handler(signal, frame):
global done
print 'You pressed Ctrl+C!'
done=True
sys.exit()
signal.signal(signal.SIGINT, signal_handler)
class ImageProcessor(threading.Thread):
def __init__(self):
super(ImageProcessor, self).__init__()
self.stream = io.BytesIO()
self.event = threading.Event()
self.terminated = False
self.daemon=True;
self.start()
def run(self):
# This method runs in a separate thread
global done
while not self.terminated:
# Wait for an image to be written to the stream
if self.event.wait(1):
try:
self.stream.seek(0)
# Read the image and do some processing on it
#Image.open(self.stream)
#...
#...
# Set done to True if you want the script to terminate
# at some point
#done=True
finally:
# Reset the stream and event
self.stream.seek(0)
self.stream.truncate()
self.event.clear()
# Return ourselves to the pool
with lock:
pool.append(self)
def streams():
while not done:
with lock:
if pool:
processor = pool.pop()
else:
processor = None
if processor:
yield processor.stream
processor.event.set()
else:
# When the pool is starved, wait a while for it to refill
time.sleep(0.1)
with picamera.PiCamera() as camera:
pool = [ImageProcessor() for i in range(4)]
camera.resolution = (640, 480)
camera.framerate = 30
camera.start_preview()
time.sleep(2)
camera.capture_sequence(streams(), use_video_port=True)
# Shut down the processors in an orderly fashion
while pool:
with lock:
processor = pool.pop()
processor.terminated = True
processor.join()
但中斷信號從未被抓住。
直到camera.capture_sequence(streams(), use_video_port=True)
運行信號被捕獲,capture_sequence
啓動後,信號處理程序不會被調用。
我是python的新手,所以答案很簡單。我在這裏做錯了什麼?
編輯:
如果我刪除下面的代碼信號被捕獲:
yield processor.stream
你說得對有關代碼更正的附錄A,但問題是SIG INT信號沒有抓住這樣的signal_handler功能是從來沒有所謂的 –
你是什麼平臺的Windows? Posix的? –