Skip to content

Instantly share code, notes, and snippets.

@greut
Created April 23, 2010 10:23
Show Gist options
  • Save greut/376416 to your computer and use it in GitHub Desktop.
Save greut/376416 to your computer and use it in GitHub Desktop.
async applications test those with `ab -n 10 -c 10` it should take around 1 sec (and not 10) : http://yoan.dosimple.ch/blog/2010/04/24/
from eventlet import sleep, wsgi, listen
def application(environ, start_response):
start_response("200 OK", [("Content-Type", "text/plain")])
sleep(1)
return ["Hello, world!"]
def main():
wsgi.server(listen(('', 8000)), application)
if __name__ == "__main__":
main()
from gevent import sleep, wsgi
def application(environ, start_response):
start_response("200 OK", [("Content-Type", "text/plain")])
sleep(1)
return ["Hello, world!"]
def main():
wsgi.WSGIServer(('', 8000), application).serve_forever()
if __name__ == "__main__":
main()
var http = require("http");
http.createServer(function(req, res){
res.writeHead(200, {"Content-Type": "text/html"});
setTimeout(function(){
res.end("Hello, world!")
},1000)
}).listen(8000);
class DeferrableBody
include EventMachine::Deferrable
def call(body)
body.each do |chunk|
@body_callback.call(chunk)
end
end
def each &block
@body_callback = block
end
end
app = proc do |env|
body = DeferrableBody.new
EventMachine::next_tick do
env["async.callback"].call [200,
{"Content-Type" => "text/html"},
body]
end
EventMachine::add_timer(1) do
body.call ["Hello, world!"]
body.succeed
end
[-1, {}, []]
end
run app
import time
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
tornado.ioloop.IOLoop.instance().add_timeout(
time.time() + 1,
self.async_callback(self.later))
def later(self):
self.write("Hello, world!")
self.finish()
urls = (
(r"/", MainHandler),
)
application = tornado.web.Application(urls)
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
from twisted.web import server, resource
from twisted.internet import reactor, task
class Root(resource.Resource):
isLeaf = True
def render_GET(self, request):
d = task.deferLater(reactor, 1, lambda: request)
d.addCallback(self.output)
return server.NOT_DONE_YET
def output(self, request):
request.write("Hello, world!")
request.finish()
site = server.Site(Root())
reactor.listenTCP(8000, site)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment