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,这就实现了协程 我们知道线程的调度(线程上下文切 ...
随机推荐
- Vim:基础
Normal模式 即是command “vim test.txt” 时进入的界面. 常用command: :help<Enter> 查看命令. :wq<Enter> ...
- js判断当前浏览类型
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串var isOpera = userAgent.indexOf("Opera ...
- 如何实现一个可以用 await 异步等待的 Awaiter
.NET 和 C# 共同给我们带来的 async/await 异步编程模型(TAP)用起来真的很爽.为了实现异步等待,我们只需要在一切能够能够异步等待的方法前面加上 await 即可.能够异步等待的最 ...
- 重温CLR(十五) 托管堆和垃圾回收
本章要讨论托管应用程序如何构造新对象,托管堆如何控制这些对象的生存期,以及如何回收这些对象的内存.简单地说,本章要解释clr中的垃圾回收期是如何工作的,还要解释相关的性能问题.另外,本章讨论了如何设计 ...
- pandas 的Series 里经常会出现DatetimeIndex这个类
DatetimeIndex 的操作还是值得研究一下的. 参考其用法, http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Date ...
- exec 和 exec_
exec_ 是six里面针对py2 , py3 兼容, 而做的重新定义. 和 exec 功能一样. 关键一点是: exec 是一个语法声明,不是一个函数. 也就是说和if,for一样. ex ...
- MySQL中地理位置数据扩展geometry的使用心得
最近学习了些MySQL geometry数据存储和计算,在这里记录下. 1. 环境 geometry推荐在5.6版本以上使用,尽管大部分功能在5.5已经可用,除了距离计算函数st_distance等新 ...
- nginx ngscript 简单使用
备注: 默认没有集成到nginx包里,需要单独安装(推荐使用动态模块的方式进行安装) 1. 安装 wget https://nginx.org/download/nginx-1.13.11.tar.g ...
- Matlab 之 FFT的理解和应用
网上看了一些大牛的关于FFT的见解,加上自己的一点儿理解,针对以下这几个问题来加深对FFT的理解. 不知道大家有没有类似以下几点的困惑: 问题的提出 对于1秒钟输出的连续信号,使用采样率Fs不同,就会 ...
- php-fpm设置与 phpMyadmin超时 操作SQL超时
LNMP 一键安装包环境: Phpmyadmin 登录超时 (1440 秒未活动),请重新登录. vim /usr/local/php/etc/php.ini session.gc_maxlife ...