From: Calin Crisan Date: Sun, 18 Oct 2015 13:43:25 +0000 (+0300) Subject: working schedule checker is now implemented using coroutines and X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=4bb9a11a904b51bc271dc3c4892139227218cac2;p=motioneye-debian working schedule checker is now implemented using coroutines and AsyncHTTPClient, to prevent memory leaks by using a regular HTTPClient --- diff --git a/motioneye/motionctl.py b/motioneye/motionctl.py index bd79cbc..3385e90 100644 --- a/motioneye/motionctl.py +++ b/motioneye/motionctl.py @@ -23,7 +23,8 @@ import signal import subprocess import time -from tornado.httpclient import HTTPClient, AsyncHTTPClient, HTTPRequest +from tornado import gen +from tornado.httpclient import AsyncHTTPClient, HTTPRequest from tornado.ioloop import IOLoop import config @@ -212,34 +213,36 @@ def started(): return _started +@gen.coroutine def get_motion_detection(camera_id): thread_id = camera_id_to_thread_id(camera_id) if thread_id is None: - return logging.error('could not find thread id for camera with id %s' % camera_id) - + logging.error('could not find thread id for camera with id %s' % camera_id) + return + url = 'http://127.0.0.1:7999/%(id)s/detection/status' % {'id': thread_id} request = HTTPRequest(url, connect_timeout=5, request_timeout=5) - http_client = HTTPClient() + http_client = AsyncHTTPClient() try: - response = http_client.fetch(request) + response = yield http_client.fetch(request) if response.error: - raise response.error - + raise response.error + except Exception as e: logging.error('failed to get motion detection status for camera with id %(id)s: %(msg)s' % { 'id': camera_id, 'msg': unicode(e)}) + + return - return None - enabled = bool(response.body.lower().count('active')) logging.debug('motion detection is %(what)s for camera with id %(id)s' % { 'what': ['disabled', 'enabled'][enabled], 'id': camera_id}) - - return enabled + + raise gen.Return(enabled) def set_motion_detection(camera_id, enabled): diff --git a/motioneye/wsswitch.py b/motioneye/wsswitch.py index be65c27..59490d0 100644 --- a/motioneye/wsswitch.py +++ b/motioneye/wsswitch.py @@ -17,7 +17,8 @@ import datetime import logging -import tornado + +from tornado import ioloop, gen import config import motionctl @@ -25,8 +26,8 @@ import utils def start(): - ioloop = tornado.ioloop.IOLoop.instance() - ioloop.add_timeout(datetime.timedelta(seconds=10), _check_ws) + io_loop = ioloop.IOLoop.instance() + io_loop.add_timeout(datetime.timedelta(seconds=1), _check_ws) def _during_working_schedule(now, working_schedule): @@ -69,14 +70,15 @@ def _during_working_schedule(now, working_schedule): return True +@gen.coroutine def _check_ws(): # schedule the next call - ioloop = tornado.ioloop.IOLoop.instance() - ioloop.add_timeout(datetime.timedelta(seconds=10), _check_ws) + io_loop = ioloop.IOLoop.instance() + io_loop.add_timeout(datetime.timedelta(seconds=10), _check_ws) if not motionctl.running(): return - + now = datetime.datetime.now() for camera_id in config.get_camera_ids(): camera_config = config.get_camera(camera_id) @@ -96,7 +98,7 @@ def _check_ws(): now_during = _during_working_schedule(now, working_schedule) must_be_enabled = (now_during and working_schedule_type == 'during') or (not now_during and working_schedule_type == 'outside') - currently_enabled = motionctl.get_motion_detection(camera_id) + currently_enabled = yield motionctl.get_motion_detection(camera_id) if currently_enabled is None: # could not detect current status logging.warn('skipping motion detection status update for camera with id %(id)s' % {'id': camera_id}) continue