串行测试

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. div横排放置对齐问题;block,inline,inline-block区别

    1.左右两个div都设置为float:left,如果右边div没有设置宽度,右边div的宽度会根据div里的内容自动调整. 缺点:不易控制 2.只有左侧div设置为float:left,右侧div设置 ...

  2. AngularX Http服务总结

    自己经常用的方式: 1.首先看httpModule Angular2的http访问经常采用两种方式: 共同点:导入相应的Module import {Http, RequestOptions, Res ...

  3. java并发--Callable、Future和FutureTask

    在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就 ...

  4. Roslyn 入门:使用 Visual Studio 的语法可视化窗格查看和了解代码的语法树

    使用 Visual Studio 提供的 Syntax Visualizer,我们可以实时看到一个代码文件中的语法树.这对我们基于 Roslyn 编写静态分析和修改工具非常有帮助.本文将介绍如何安装它 ...

  5. python函数参数总结

    python中函数参数有:默认参数.关键字参数.非关键字可变长参数(元组).关键字可变长参数(字典) 默认参数:在函数声明时,指定形参的默认值,调用时可不传入改参数(使用默认值)def foo(x): ...

  6. select rows by values in a column from Dataframe

    df.loc[df['column_name'] == some_value] details in: http://stackoverflow.com/questions/17071871/sele ...

  7. ballerina 学习二 ballerina 命令参数

    1.  目前支持的命令 run Run Ballerina program build Compile Ballerina program install Install packages to ho ...

  8. Javascript实现简单的选项卡

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011043843/article/details/30723933 在线演示:http://jsf ...

  9. C#开机启动与退出程序

    最新用到的项目中需要使用开机自启和退出程序,于是需要 http://www.cnblogs.com/Gaoswatou/p/6605760.html C# WinForm程序退出的方法 1.this. ...

  10. 基于IAR和STM32的uCOS-II移植

    网上基于MDK的移植数不胜数,但是基于IAR的移植几乎没有,因为官方的例程就是基于IAR的,所以移植起来很简单,没人介绍,但还是得小心谨慎,一不小心就出错,对于新手来说,查找错误可不是那么容易的.IA ...