串行测试

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()

python 协程 gevent 简单测试的更多相关文章

  1. python协程的简单了解

    协程: 协程,又称微线程,纤程.英文名Coroutine. 可以在不同的函数间切换,而且切换的次数和时间都是由用户自己确定的. 协程的几种实现方式: (1)使用生成器yield实现. 如果不了解生成器 ...

  2. python协程gevent案例:爬取斗鱼美女图片

    分析 分析网站寻找需要的网址 用谷歌浏览器摁F12打开开发者工具,然后打开斗鱼颜值分类的页面,如图: 在里面的请求中,最后发现它是以ajax加载的数据,数据格式为json,如图: 圈住的部分是我们需要 ...

  3. Python协程 Gevent Eventlet Greenlet

    https://zh.wikipedia.org/zh-cn/%E5%8D%8F%E7%A8%8B 协程可以理解为线程中的微线程,通过手动挂起函数的执行状态,在合适的时机再次激活继续运行,而不需要上下 ...

  4. python协程详解,gevent asyncio

    python协程详解,gevent asyncio 新建模板小书匠 #协程的概念 #模块操作协程 # gevent 扩展模块 # asyncio 内置模块 # 基础的语法 1.生成器实现切换 [1] ...

  5. python编程中的并发------协程gevent模块

    任务例子:喝水.吃饭动作需要耗时1S 单任务:(耗时20s) for i in range(10): print('a正在喝水') time.sleep(1) print('a正在吃饭') time. ...

  6. Python 协程并发爬虫网页

    简单爬虫实例: 功能:通过urllib.request实现网站爬虫,捕获网站内容. from urllib import request def f(url): print("GET:%s& ...

  7. python2.0_s12_day9_协程&Gevent协程

    Python之路,Day9 - 异步IO\数据库\队列\缓存 本节内容 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 协程 1.协程,又 ...

  8. day-5 python协程与I/O编程深入浅出

    基于python编程语言环境,重新学习了一遍操作系统IO编程基本知识,同时也学习了什么是协程,通过实际编程,了解进程+协程的优势. 一.python协程编程实现 1.  什么是协程(以下内容来自维基百 ...

  9. 终结python协程----从yield到actor模型的实现

    把应用程序的代码分为多个代码块,正常情况代码自上而下顺序执行.如果代码块A运行过程中,能够切换执行代码块B,又能够从代码块B再切换回去继续执行代码块A,这就实现了协程 我们知道线程的调度(线程上下文切 ...

随机推荐

  1. [译]缓解BEAST对TLS攻击的方式

    原文链接:https://community.qualys.com/blogs/securitylabs/2011/10/17/mitigating-the-beast-attack-on-tls 原 ...

  2. 用stack实现min stack

    遇到个好玩的问题,就是用一个stack实现min stack,什么意思呢,就是我实现stack,但是能以O(1)的时间复杂度和空间复杂度去找到我stack里面的最小值. 常规的方法是:用一个变量存放当 ...

  3. Spring 管理Filter和Servlet

    本文转载自:http://www.open-open.com/lib/view/open1417248512252.html 在使用spring容器的web应用中,业务对象间的依赖关系都可以用cont ...

  4. cannot find lstdc++

    centos下编译grpc-java时遇到的问题,google了一下 : sudo yum install libstdc++-static

  5. 《DSP using MATLAB》示例Example7.3

    由图上可以看出,与幅度谱对应的相位谱是分段线性函数,而与振幅谱对应的相位谱是真正线性函数. 幅度谱和振幅谱的区别也很明显.

  6. CycloneII之EDA及学术开发功能描述

    1.概述 同Stratix/Cyclone. 2.逻辑单元(Logic Cell)描述 在以前的架构中(比如Cyclone),单个LE包括一个组合逻辑和寄存器.对于Cyclone II来说,组合逻辑和 ...

  7. python-pycharm控制台输出带颜色

    python_控制台输出带颜色的文字方法   在python开发的过程中,经常会遇到需要打印各种信息.海量的信息堆砌在控制台中,就会导致信息都混在一起,降低了重要信息的可读性.这时候,如果能给重要的信 ...

  8. 关于时间戳和QDateTime相互转换的有关问题(转)

    1.toTime_t()把2014年12月19日10:24:40这样的QDateTime的格式转变为1418955940这样的时间戳 QDateTime time = QDateTime::curre ...

  9. 关于angular的好文推荐

    独立作用域篇 1)http://www.angularjs.cn/A09C 2)http://www.cnblogs.com/wangmeijian/p/4944030.html 理解$watch , ...

  10. WiresShark使用说明

    WiresShark是号称全世界最流行的网络分析工具(它的官网自己说的).下载地址:https://www.wireshark.org/#download,目前最新版本是2.6.2.我本地用的是汉化的 ...