zoukankan      html  css  js  c++  java
  • python 协程 gevent 简单测试

    串行测试

    from gevent import monkey; monkey.patch_all()#有IO才做时需要这一句
    import gevent
    import requests,time
    
    start=time.time()
    def f(url):
        print('GET: %s' % url)
        resp =requests.get(url)
        data = resp.text
        print('%d bytes received from %s.' % (len(data), url))
    
    f('http://127.0.0.1:8001/s')
    f('http://127.0.0.1:8001/s')
    f('http://127.0.0.1:8001/s')
    
    
    print("cost time:",time.time()-start)
    
    
    #输入  共用 15s
    #GET: http://127.0.0.1:8001/s
    #13 bytes received from http://127.0.0.1:8001/s.
    #GET: http://127.0.0.1:8001/s
    #13 bytes received from http://127.0.0.1:8001/s.
    #GET: http://127.0.0.1:8001/s
    #13 bytes received from http://127.0.0.1:8001/s.
    #cost time: 15.034623622894287

    并行测试

    from gevent import monkey; monkey.patch_all()#有IO才做时需要这一句
    import gevent
    import requests,time
    
    start=time.time()
    def f(url):
        print('GET: %s' % url)
        resp =requests.get(url)
        data = resp.text
        print('%d bytes received from %s.' % (len(data), url))
    
    gevent.joinall([
            gevent.spawn(f, 'http://127.0.0.1:8001/s'),
            gevent.spawn(f, 'http://127.0.0.1:8001/s'),
            gevent.spawn(f, 'http://127.0.0.1:8001/s'),
    ])
    
    print("cost time:",time.time()-start)
    
    # 输出 共用5s
    # GET: http://127.0.0.1:8001/s
    # GET: http://127.0.0.1:8001/s
    # GET: http://127.0.0.1:8001/s
    # 13 bytes received from http://127.0.0.1:8001/s.
    # 13 bytes received from http://127.0.0.1:8001/s.
    # 13 bytes received from http://127.0.0.1:8001/s.
    # cost time: 5.034070014953613

    被请求的tornado代码

    sleep 5s

    #!/bin/env python
    import tornado.httpserver
    import tornado.ioloop
    import tornado.options
    import tornado.web
    import tornado.httpclient
    import tornado.gen
    from tornado.concurrent import run_on_executor
    from concurrent.futures import ThreadPoolExecutor
    
    import time
    
    from tornado.options import define, options
    define("port", default=8001, help="run on the given port", type=int)
    
    class SleepHandler(tornado.web.RequestHandler):
        executor = ThreadPoolExecutor(4)
        @tornado.web.asynchronous
        @tornado.gen.coroutine
        def get(self):
            res = yield self.sleep()
            self.write("when i sleep ")
            self.finish()
    
        @run_on_executor
        def sleep(self):
            time.sleep(5)
            return 5
    
    
    if __name__ == "__main__":
        tornado.options.parse_command_line()
        app = tornado.web.Application(handlers=[
                (r"/s", SleepHandler)])
        http_server = tornado.httpserver.HTTPServer(app)
        http_server.listen(options.port)
        tornado.ioloop.IOLoop.instance().start()
  • 相关阅读:
    无锁并行框架构建复杂消费模型
    Disruptor框架EventProcessor和Workpool的使用
    .NET工作准备--04ASP.NET
    .NET工作准备--03进阶知识
    .NET工作准备--02基础知识
    .NET工作准备--01前言
    Java核心编程快速入门
    IntellijIDEA快速入门(Windows版)
    企业模式和设计模式快速入门
    架构设计深入学习02-概念架构与细化架构
  • 原文地址:https://www.cnblogs.com/xiaoshi657/p/9034307.html
Copyright © 2011-2022 走看看