GithubHelp home page GithubHelp logo

myasynchttp's Introduction

MyAsyncHttp: 从零实现一个简单的异步http框架

  • 由于本项目之目的是研究异步编程的底层原理,故未使用python原生的asyncio以及其他第三方库,单纯使用socket+epoll实现
  • 最初实现的版本只支持回调的代码结构。为避免层层回调(所谓“回调地狱”)带来的编码困难,开发了协程版本,支持yield from语法。
  • 默认使用的是协程版本,如果要使用回调版本,则需要from MyAsyncHttp.callback_version import Server
  • 在单线程异步框架中,必须处处异步,否则将阻塞线程。故在async_io_utils包中,简单实现了一个异步http请求库。后续将考虑加入异步文件IO的支持。

代码用例:

import logging
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')


from MyAsyncHttp import Server,Request
from MyAsyncHttp import async_io

server = Server('127.0.0.1', 8000)

@server.router.rule("/", method="GET")
def index(request: Request):
    return "welcome to index"

@server.router.rule("/hello", method="GET")
def hello(request: Request):
    name = request.params['name']
    return "hello," + name

@server.router.rule("/baidu", method="GET")
def baidu(request: Request):
    url = "http://www.baidu.com"
    res_header, res_body = yield from async_io.http_request.request("GET", url, header=[])
    return res_body


server.start()

myasynchttp's People

Contributors

heiwushi avatar

Watchers

 avatar

myasynchttp's Issues

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.