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()
python 协程 gevent 简单测试的更多相关文章
- python协程的简单了解
协程: 协程,又称微线程,纤程.英文名Coroutine. 可以在不同的函数间切换,而且切换的次数和时间都是由用户自己确定的. 协程的几种实现方式: (1)使用生成器yield实现. 如果不了解生成器 ...
- python协程gevent案例:爬取斗鱼美女图片
分析 分析网站寻找需要的网址 用谷歌浏览器摁F12打开开发者工具,然后打开斗鱼颜值分类的页面,如图: 在里面的请求中,最后发现它是以ajax加载的数据,数据格式为json,如图: 圈住的部分是我们需要 ...
- Python协程 Gevent Eventlet Greenlet
https://zh.wikipedia.org/zh-cn/%E5%8D%8F%E7%A8%8B 协程可以理解为线程中的微线程,通过手动挂起函数的执行状态,在合适的时机再次激活继续运行,而不需要上下 ...
- python协程详解,gevent asyncio
python协程详解,gevent asyncio 新建模板小书匠 #协程的概念 #模块操作协程 # gevent 扩展模块 # asyncio 内置模块 # 基础的语法 1.生成器实现切换 [1] ...
- python编程中的并发------协程gevent模块
任务例子:喝水.吃饭动作需要耗时1S 单任务:(耗时20s) for i in range(10): print('a正在喝水') time.sleep(1) print('a正在吃饭') time. ...
- Python 协程并发爬虫网页
简单爬虫实例: 功能:通过urllib.request实现网站爬虫,捕获网站内容. from urllib import request def f(url): print("GET:%s& ...
- python2.0_s12_day9_协程&Gevent协程
Python之路,Day9 - 异步IO\数据库\队列\缓存 本节内容 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 协程 1.协程,又 ...
- day-5 python协程与I/O编程深入浅出
基于python编程语言环境,重新学习了一遍操作系统IO编程基本知识,同时也学习了什么是协程,通过实际编程,了解进程+协程的优势. 一.python协程编程实现 1. 什么是协程(以下内容来自维基百 ...
- 终结python协程----从yield到actor模型的实现
把应用程序的代码分为多个代码块,正常情况代码自上而下顺序执行.如果代码块A运行过程中,能够切换执行代码块B,又能够从代码块B再切换回去继续执行代码块A,这就实现了协程 我们知道线程的调度(线程上下文切 ...
随机推荐
- Django项目部署(阿里云)(1)--基本功能实现
新博客地址:http://muker.net/django-server.html 手头需要部署一个Django项目,前面的博客也因为偷懒也没有部署,这里记录一下部署过程.ps:其实网上比较靠谱的说明 ...
- bzoj 4447 小凸解密码
bzoj 4447 小凸解密码 先将原始状态的 \(B\) 处理出来,可以发现,若不修改,则每次指定的起始位置不同,对这个环 \(B\) 带来的影响只有 \(B_0\) 不同,即每次 \(B_0=A_ ...
- MySQL 字段基本操作
MySQL添加字段的方法并不复杂,下面将为您详细介绍MySQL添加字段和修改字段等操作的实现方法,希望对您学习MySQL添加字段方面会有所帮助. .登录数据库 >mysql -u root -p ...
- jQuery AJAX 跨域请求
跨域请求 只要 dataType : 'jsonp', jsonp:"jsoncallback", 然后返回一个json格式的就可以了 <!doctype html> ...
- db_recovery_file_dest_size 修改大一点及删除归档日志 |转|
今天给客户测 试问题,让客户把数据发过来了.解压缩后一看,他们还是用的oracle 815版本的(他们exp导出时,带了导出日志,从导出日志中看出来是oracle 815版本的),不过没有关系,低版本 ...
- C/C++中一些不太注意到的小知识点--[锦集]
C/C++中一些不太注意到的小知识点--[锦集] C/C++小知识点--[锦集] "="和"<=" 的优先级 1.( (file_got_len = re ...
- centos 限制只能访问某个目录的php文件
vi /etc/php.ini #编辑 open_basedir = .:/tmp/ #在380行 设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果 ...
- 关于cookie你不知道的
cookie虽然在持久保存客户端数据提供了方便,分担了服务器存储的负担,但还是有很多局限性的.第一:每个特定的域名下最多生成20个cookie1.IE6或更低版本最多20个cookie2.IE7和之后 ...
- STM32之中断
在STM32(Cortex-M3)中没有显示的代码拷贝,只有启动代码进行了向量的初始化,一直以为是编译器在程序影像中自己完成了相关向量的拷贝,即,拷贝到固定的NVIC区,事实上并不是这样,cortex ...
- DS04--树
一.学习总结 1.树结构思维导图 2.树结构学习体会 树这一节遇到最大的困难就是递归不能灵活的运用,总是想用链表那里的知识解决,做了一大堆,程序崩溃也找不到问题出在哪里. 二.PTA实验作业 题目1: ...