Skip to content

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders Jensen committed Apr 29, 2018
2 parents 03ce654 + ca4f5e9 commit dff86b4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
@@ -1,6 +1,12 @@
Changelog
=========

Version 2.1.0 (29-04-2018)
-----------------------------------------------------------

* Updating to support Channels 2.1.0 and the upcoming
ASGI spec changes (run application initialization in a thread)

Version 2.0.1 (27-04-2018)
-----------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion txasgiresource/__init__.py
Expand Up @@ -9,4 +9,4 @@
except ImportError:
pass

__version__ = '2.0.1'
__version__ = '2.1.0'
5 changes: 3 additions & 2 deletions txasgiresource/application.py
@@ -1,7 +1,7 @@
import asyncio
from concurrent.futures import CancelledError

from twisted.internet import defer
from twisted.internet import defer, threads


class ApplicationManager:
Expand All @@ -22,11 +22,12 @@ def stop(self):
except CancelledError:
pass

@defer.inlineCallbacks
def create_application_instance(self, protocol, scope):
async def handle_reply(msg):
protocol.handle_reply(msg)

application_instance = self.application(scope)
application_instance = yield threads.deferToThread(self.application, scope)
queue = asyncio.Queue()

self.application_instances[protocol] = asyncio.ensure_future(
Expand Down
8 changes: 6 additions & 2 deletions txasgiresource/http.py
Expand Up @@ -113,7 +113,8 @@ def handle_reply(self, msg):
self.reply_defer = defer.Deferred()
d.callback(msg)

def render(self, request):
@defer.inlineCallbacks
def _render(self, request):
self.request = request

scope = dict(self.base_scope)
Expand All @@ -122,10 +123,13 @@ def render(self, request):
scope['scheme'] = 'http%s' % (scope.pop('_ssl'))
scope['method'] = request.method.decode('utf8')

self.queue = self.application.create_application_instance(self, scope)
self.queue = yield self.application.create_application_instance(self, scope)

self.send_request_to_application(request, request.content)

def render(self, request):
self._render(request)

return server.NOT_DONE_YET

@defer.inlineCallbacks
Expand Down
20 changes: 12 additions & 8 deletions txasgiresource/ws.py
Expand Up @@ -14,19 +14,15 @@ class ASGIWebSocketServerProtocol(WebSocketServerProtocol, policies.TimeoutMixin
accept_promise = None
queue = None

def onConnect(self, request):
self.request = request
self.setTimeout(self.factory.idle_timeout)
self.accept_promise = defer.Deferred()
self.reply_defer = defer.Deferred()

@defer.inlineCallbacks
def _onConnect(self, request):
scope = dict(self.factory.base_scope)
scope['type'] = 'websocket'
scope['scheme'] = 'ws%s' % (scope.pop('_ssl'))
# # TODO: add subprotocols
# TODO: add subprotocols

try:
self.queue = self.factory.application.create_application_instance(self, scope)
self.queue = yield self.factory.application.create_application_instance(self, scope)
self.opened = True
except Exception as e:
logger.exception('Failed to create application')
Expand All @@ -36,6 +32,14 @@ def onConnect(self, request):

self.send_replies()

def onConnect(self, request):
self.request = request
self.setTimeout(self.factory.idle_timeout)
self.accept_promise = defer.Deferred()
self.reply_defer = defer.Deferred()

self._onConnect(request)

return self.accept_promise

@defer.inlineCallbacks
Expand Down

0 comments on commit dff86b4

Please sign in to comment.