从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的? –