Commit bded1c1b authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

@defer.inlineCallbacks support

parent a7e87051
...@@ -30,11 +30,22 @@ Example: ...@@ -30,11 +30,22 @@ Example:
if __name__ == '__main__': if __name__ == '__main__':
app.run() app.run()
@defer.inlineCallbacks support
------------------------------
If you want a deferred async method, just complete the request yourself, instead of returning a string response
@app.route("/",Http.GET)
@defer.inlineCallbacks
def root(request):
val = yield db.query("SELECT ....")
request.write(val)
request.finish()
Performance Performance
----------- -----------
Pushing 8,000+ TPS on a simple 'Hello World' app using 'ab -n 100000 -c 200' On par with raw *twisted.web* performance. Minimal overhead for URL routing and function argument extraction.
for benchmarking while running on PyPy 1.6
Plans Plans
----- -----
......
...@@ -4,9 +4,9 @@ Main server classes ...@@ -4,9 +4,9 @@ Main server classes
@author: jacekf @author: jacekf
''' '''
import re, copy import re, copy
from twisted.internet import reactor from twisted.internet import reactor, defer
from twisted.web.resource import Resource from twisted.web.resource import Resource
from twisted.web.server import Site from twisted.web.server import Site, NOT_DONE_YET
from collections import defaultdict from collections import defaultdict
from enums import MediaType from enums import MediaType
from corepost.enums import Http from corepost.enums import Http
...@@ -165,7 +165,15 @@ class CorePost(Resource): ...@@ -165,7 +165,15 @@ class CorePost(Resource):
# if POST/PUT, check if we need to automatically parse JSON # if POST/PUT, check if we need to automatically parse JSON
# TODO # TODO
return urlrouter.call(request,**allargs) #handle Deferreds natively
val = urlrouter.call(request,**allargs)
if isinstance(val,defer.Deferred):
# we assume the method will call request.finish()
return NOT_DONE_YET
else:
return val
else: else:
return self.__renderError(request,404,"URL '%s' not found\n" % request.path) return self.__renderError(request,404,"URL '%s' not found\n" % request.path)
......
...@@ -5,12 +5,16 @@ Server tests ...@@ -5,12 +5,16 @@ Server tests
from corepost.server import CorePost from corepost.server import CorePost
from corepost.enums import Http from corepost.enums import Http
from twisted.internet import defer
app = CorePost() app = CorePost()
@app.route("/",Http.GET) @app.route("/",Http.GET)
@defer.inlineCallbacks
def root(request,**kwargs): def root(request,**kwargs):
return "%s" % kwargs yield 1
request.write("%s" % kwargs)
request.finish()
@app.route("/test",Http.GET) @app.route("/test",Http.GET)
def test(request,**kwargs): def test(request,**kwargs):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment