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,这就实现了协程 我们知道线程的调度(线程上下文切 ...
随机推荐
- 自己理解的java工厂模式,希望对大家有所帮助
[http://www.360doc.com/content/11/0824/17/3034429_142983837.shtml] 这两天突然想学学java源代码,不过看到一篇文章说看java源代码 ...
- Codeforces 28C Bath Queue 【计数类DP】*
Codeforces 28C Bath Queue LINK 简要题意:有 n 个人等概率随机进入 m 个房间,一个房间可以有多个人,第 i 个房间有 ai 个水龙头,在一个房间的人要去排队装水,他们 ...
- BZOJ1486 HNOI2009 最小圈 【01分数规划】
BZOJ1486 HNOI2009 最小圈 Description 应该算是01分数规划的裸板题了吧..但是第一次写还是遇到了一些困难,vis数组不清零之类的 假设一个答案成立,那么一定可以找到一个环 ...
- swing自定义JDialog弹出框
第一次搞swing,自定义JDialog的例子较少,写下来备忘 ,对JDialog中的文本框进行了验证 package com.chauvet; import java.awt.Component; ...
- flask第十四篇——重定向
我们都知道京东的url是www.jd.com,但是当你输入www.jingdong.com时候,你会发现地址自动跳转到了www.jd.com,这种技术手段就叫做重定向. 重定向分为永久重定向和临时重定 ...
- SpringBoot 接收 单个String入参之解决方案
场景: 在做接口时,有的时候,接口入参只需要一个参数,如果将一个参数封装成一个对象很麻烦,故有了以下方式: 思路: spring自带的参数解析器貌似是不具备这个能力的,所有自定义 方式方法: 1.定义 ...
- 记一次socket_create()函数耗时异常记录
背景: 下午开发时突然整个页面耗时增加,空接口每次都需要2-3秒的耗时,一开始以为连开发环境数据库出现问题,最后断开数据库跑,发现还是很慢 最终逐步调试此页面耗时,定位到了socket_create( ...
- 使用zabbix监控mysql
系统版本: centos6 x86_64 mysql版本: mysql5.6 实施目的: 监控mysql 客户端配置: 1.准备工作:搭建zabbix服务,使服务端客户端连接成功,并有基础监控项 2. ...
- python3之es+log+date+timezone
from dateutil.parser import parse # 使用它可以方便的将字符串解析为datetimefrom tzlocal import get_localzone # 使用它可以 ...
- 老司机带带我,FIFO不简单
FIFO,没有想象的那么简单! 根据一般的原则,FIFO这件事情啊,如果能用IP先别自己手写.可以通过FPGA厂家的的IP生成工具生成相应的FIFO. FIFO中的格雷码: 格雷码应用于异步FIFO的 ...