From 7a1b914824e3dc8fcb8eb87ed44f22e7fa7adbbc Mon Sep 17 00:00:00 2001 From: Ruud Date: Sun, 21 Dec 2014 20:19:53 +0100 Subject: [PATCH] Return nonblock results in main thread --- couchpotato/api.py | 26 ++++++++++++-------------- couchpotato/core/notifications/core/main.py | 3 ++- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/couchpotato/api.py b/couchpotato/api.py index 4efd603..b5754d8 100644 --- a/couchpotato/api.py +++ b/couchpotato/api.py @@ -51,24 +51,22 @@ class NonBlockHandler(RequestHandler): start, stop = api_nonblock[route] self.stopper = stop - start(self.onNewMessage, last_id = self.get_argument('last_id', None)) + start(self.sendData, last_id = self.get_argument('last_id', None)) - def onNewMessage(self, response): - if self.request.connection.stream.closed(): - self.on_connection_close() - return - - try: - self.finish(response) - except: - log.debug('Failed doing nonblock request, probably already closed: %s', (traceback.format_exc())) - try: self.finish({'success': False, 'error': 'Failed returning results'}) - except: pass + def sendData(self, response): + if not self.request.connection.stream.closed(): + try: + self.finish(response) + except: + log.debug('Failed doing nonblock request, probably already closed: %s', (traceback.format_exc())) + try: self.finish({'success': False, 'error': 'Failed returning results'}) + except: pass - def on_connection_close(self): + self.removeStopper() + def removeStopper(self): if self.stopper: - self.stopper(self.onNewMessage) + self.stopper(self.sendData) self.stopper = None diff --git a/couchpotato/core/notifications/core/main.py b/couchpotato/core/notifications/core/main.py index 4055c41..771d969 100644 --- a/couchpotato/core/notifications/core/main.py +++ b/couchpotato/core/notifications/core/main.py @@ -14,6 +14,7 @@ from couchpotato.core.logger import CPLog from couchpotato.core.notifications.base import Notification from .index import NotificationIndex, NotificationUnreadIndex from couchpotato.environment import Env +from tornado.ioloop import IOLoop log = CPLog(__name__) @@ -190,7 +191,7 @@ class CoreNotifier(Notification): while len(self.listeners) > 0 and not self.shuttingDown(): try: listener, last_id = self.listeners.pop() - listener({ + IOLoop.current().add_callback(listener, { 'success': True, 'result': [notification], })