Return nonblock results in main thread

This commit is contained in:
Ruud
2014-12-21 20:19:53 +01:00
parent 5e62801666
commit 7a1b914824
2 changed files with 14 additions and 15 deletions
+12 -14
View File
@@ -51,24 +51,22 @@ class NonBlockHandler(RequestHandler):
start, stop = api_nonblock[route] start, stop = api_nonblock[route]
self.stopper = stop 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): def sendData(self, response):
if self.request.connection.stream.closed(): if not self.request.connection.stream.closed():
self.on_connection_close() try:
return 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
try: self.removeStopper()
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):
def removeStopper(self):
if self.stopper: if self.stopper:
self.stopper(self.onNewMessage) self.stopper(self.sendData)
self.stopper = None self.stopper = None
+2 -1
View File
@@ -14,6 +14,7 @@ from couchpotato.core.logger import CPLog
from couchpotato.core.notifications.base import Notification from couchpotato.core.notifications.base import Notification
from .index import NotificationIndex, NotificationUnreadIndex from .index import NotificationIndex, NotificationUnreadIndex
from couchpotato.environment import Env from couchpotato.environment import Env
from tornado.ioloop import IOLoop
log = CPLog(__name__) log = CPLog(__name__)
@@ -190,7 +191,7 @@ class CoreNotifier(Notification):
while len(self.listeners) > 0 and not self.shuttingDown(): while len(self.listeners) > 0 and not self.shuttingDown():
try: try:
listener, last_id = self.listeners.pop() listener, last_id = self.listeners.pop()
listener({ IOLoop.current().add_callback(listener, {
'success': True, 'success': True,
'result': [notification], 'result': [notification],
}) })