-
-
Save evangineer/413253 to your computer and use it in GitHub Desktop.
async app example code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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