Giter Site home page Giter Site logo

asyncwsgi's Introduction

asyncwsgi

asyncwsgi is a simple package for tornado and asyncio(WIP) to enable the use of the WSGI interface asynchronously. Behind the scenes greenlet is used to pause and resume each request.

Usage

  1. Decorate functions that require async with asyncwsgi.coroutine
  2. Use asyncwsgi.wrap to wrap the WSGI container.
  3. Run the event loop using asyncwsgi.run

Examples

Django

@asyncwsgi.coroutine
def my_view(request):
    http_client = AsyncHTTPClient()
    response = yield http_client.fetch('http://www.google.com/')
    return render(request, 'my_view.html', {'code': response.code})

...
# Patch django to run on tornado's event loop
def run(host, port, app, **options):
    container = asyncwsgi.wrap(WSGIContainer(app))
    http_server = HTTPServer(container)
    http_server.listen(port, host)
    asyncwsgi.run(IOLoop.current())

if __name__ == "__main__":
    ...
    from django.core.management.commands import runserver
    runserver.run = run

Bottle

import asyncwsgi
import bottle
from tornado.httpclient import AsyncHTTPClient
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop

@bottle.get('/')
@asyncwsgi.coroutine
def index():
    http_client = AsyncHTTPClient()
    response = yield http_client.fetch('http://www.google.com/')
    return 'Status: %d' % response.code

container = asyncwsgi.wrap(WSGIContainer(bottle.default_app()))
http_server = HTTPServer(container)
http_server.listen(8080)
asyncwsgi.run(IOLoop.current())

Installation

pip install asyncwsgi

Todo

  • asyncio not fully supported

License

MIT licensed. See LICENSE for details.

asyncwsgi's People

Contributors

nehz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.