大部分Web应用(包括我们之前的例子)都是阻塞性质的,也就是说当一个请求被处理时,这个进程就会被挂起直至请求完成。在大多数情况下,Tornado处理的Web请求完成得足够快使得这个问题并不需要被关注。然而,对于那些需要一些时间来完成的操作(像大数据库的请求或外部API),这意味着应用程序被有效的锁定直至处理结束,很明显这在可扩展性上出现了问题。

不过tornado给了我们更好的方法来处理这种情况。应用程序在等待第一个处理完成的过程中,让I/O循环打开以便服务于其他客户端,直到处理完成时启动一个请求并给予反馈,而不再是等待请求完成的过程中挂起进程。

我们首先构造一个阻塞性质的例子来看下:

在这个例子中我们首先需要实例化一个HTTPClient类,然后调用对象的fetch方法。通过fetch方法我们构建一个URLL来抓取阿里云根据地区名获取经纬度接口。网站会返回一个json的结果,我们将这个json结果呈现在我们的网页上

通过阿里云查询成都的经纬度

我们的代码构造如下:

通过获取网页传递的参数a来得到从查询的城市,并且构造完整的URL。response获取到网页反馈的信息并且反馈到网页上

class indexHandler(tornado.web.RequestHandler):

def get(self, *args, **kwargs):

query=self.get_argument('a').encode('utf-8')

client=tornado.httpclient.HTTPClient()

response=client.fetch("http://gc.ditu.aliyun.com/geocoding?"+urllib.urlencode({'a':query}))

body=json.loads(response.body)

self.write(response.body)

得到的结果如下。

从tornado的后台打印中看到整个的响应时延是96.78ms.

[I 171231 14:44:18 web:2063] 200 GET /?a=%E6%88%90%E9%83%BD (127.0.0.1) 96.78ms

到这里,我们已经编写了一个请求阿里云反馈城市经纬的应用。但是只有一个请求也就是可以认为是单线程应用,如果我们有多个请求的时候,性能会如何呢?

我们需要使用一个测试工具来测试大量请求的时候性能表现。这里我们用到siege工具进行测试。使用方法很简单:

siege http://127.0.0.1:8000/?a=成都 -c10 -t10s

参数的解释如下:-c代表的是设置的同时并发的用户数。

-c NUM, --concurrent=NUM

This option allows you to set the concurrent number of users. The

total number of users is technically limited to your computer's

resources.

You should not configure more users than your web server is

configured to handle. For example, the default apache configuration

is capped at 255 threads. If you run siege with -c 1024, then 769

siege users are left waiting for an apache handler.

For this reason, the default siege configuration is capped at 255

users.  You can increase that number inside s

-t代表的是测试运行的时间,-t10s代表运行10秒的意思

-t NUMm, --time=NUMm

This option is similar to --reps but instead of specifying the

number of times each user should run, it specifies the amount of

time each should run.

The value format is "NUMm", where "NUM" is an amount of time and

the "m" modifier is either S, M, or H for seconds, minutes and

hours. To run siege for an hour, you could select any one of the

following combinations: -t3600S, -t60M, -t1H.  The modifier is not

case sensitive, but it does require no space between the number and

itself.

执行结果如下:可以看到成功发送请求65次。不到10秒的时间平均响应时间达到了1.03m秒,

root@zhf-maple:/home/zhf/桌面# siege http://127.0.0.1:8000/?a=成都 -c10 -t10s

** SIEGE 4.0.2

** Preparing 10 concurrent users for battle.

The server is now under siege...

Lifting the server siege...

Transactions:           65 hits

Availability:        74.71 %

Elapsed time:         9.27 secs

Data transferred:         0.01 MB

Response time:         1.03 secs

Transaction rate:         7.01 trans/sec

Throughput:         0.00 MB/sec

Concurrency:         7.21

Successful transactions:          65

Failed transactions:           22

Longest transaction:         1.40

Shortest transaction:         0.47

我们加大同时复用的次数:在这里设置为20个并发用法。可以看到平均相应时间直接翻倍成2.29秒

root@zhf-maple:/home/zhf/桌面# siege http://127.0.0.1:8000/?a=成都 -c20 -t10s

** SIEGE 4.0.2

** Preparing 20 concurrent users for battle.

The server is now under siege...

Lifting the server siege...

Transactions:           70 hits

Availability:        76.92 %

Elapsed time:         9.89 secs

Data transferred:         0.01 MB

Response time:         2.29 secs

Transaction rate:         7.08 trans/sec

Throughput:         0.00 MB/sec

Concurrency:        16.22

Successful transactions:          70

Failed transactions:           21

Longest transaction:         2.56

Shortest transaction:         0.10

如果将并发次数继续往上抬升可以看到响应时间跟随一起增加。当我们增加到100个用户的时候,平均响应时间变成了6.12秒。

root@zhf-maple:/home/zhf/桌面# siege http://127.0.0.1:8000/?a=成都 -c100 -t10s

** SIEGE 4.0.2

** Preparing 100 concurrent users for battle.

The server is now under siege...

Lifting the server siege...

Transactions:           71 hits

Availability:        78.02 %

Elapsed time:         9.49 secs

Data transferred:         0.01 MB

Response time:         6.12 secs

Transaction rate:         7.48 trans/sec

Throughput:         0.00 MB/sec

Concurrency:        45.77

Successful transactions:          71

Failed transactions:           20

Longest transaction:         9.39

Shortest transaction:         0.16

这种增长的速度对于同时大量用户在线访问的时候肯定是无法接收的。幸运的是,Tornado包含一个AsyncHTTPClient类,可以执行异步HTTP请求。代码修改如下。首先使用tornado.web.asynchronous装饰器并在fetch增加回调函数。这个回调函数将在HTTP请求完成时被调用。并用response作为参数。

class indexHandler(tornado.web.RequestHandler):

@tornado.web.asynchronous

def get(self, *args, **kwargs):

query=self.get_argument('a').encode('utf-8')

client=tornado.httpclient.AsyncHTTPClient()

response=client.fetch("http://gc.ditu.aliyun.com/geocoding?"+urllib.urlencode({'a':query}),callback=self.on_response)

def on_response(self,response):

body=json.loads(response.body)

self.write(response.body)

self.finish()

我们来看下性能如何:同样设置-c10 -t10s。响应时间从之前的1.03秒下降到0.19秒。总共传输的次数以及每秒传输的次数也增加

root@zhf-maple:/home/zhf/桌面# siege http://127.0.0.1:8003/?a=成都 -c10 -t10s

** SIEGE 4.0.2

** Preparing 10 concurrent users for battle.

The server is now under siege...

Lifting the server siege...

Transactions:          211 hits

Availability:       100.00 %

Elapsed time:         9.36 secs

Data transferred:         0.00 MB

Response time:         0.19 secs

Transaction rate:        22.54 trans/sec

Throughput:         0.00 MB/sec

Concurrency:         4.39

Successful transactions:         211

Failed transactions:            0

Longest transaction:         0.25

Shortest transaction:         0.17

并发20个用户的时候,响应时间从2.29秒下降为0.22秒

root@zhf-maple:/home/zhf/桌面# siege http://127.0.0.1:8003/?a=成都 -c20 -t10s

** SIEGE 4.0.2

** Preparing 20 concurrent users for battle.

The server is now under siege...

Lifting the server siege...

Transactions:          389 hits

Availability:       100.00 %

Elapsed time:         9.23 secs

Data transferred:         0.01 MB

Response time:         0.22 secs

Transaction rate:        42.15 trans/sec

Throughput:         0.00 MB/sec

Concurrency:         9.38

Successful transactions:         389

Failed transactions:            0

Longest transaction:         0.44

Shortest transaction:         0.1

并发100个用户的时候,响应时间从6.12变成了1.99秒。且成功传输次数增长到了362次

root@zhf-maple:/home/zhf/桌面# siege http://127.0.0.1:8003/?a=成都 -c100 -t10s

** SIEGE 4.0.2

** Preparing 100 concurrent users for battle.

The server is now under siege...

Lifting the server siege...

Transactions:          362 hits

Availability:       100.00 %

Elapsed time:         9.19 secs

Data transferred:         0.01 MB

Response time:         1.99 secs

Transaction rate:        39.39 trans/sec

Throughput:         0.00 MB/sec

Concurrency:        78.53

Successful transactions:         362

Failed transactions:            0

Longest transaction:         3.26

Shortest transaction:         0.21

从这里可以看到,并发带来的性能提升是巨大的。特别是在多用户同时访问的情下。下一章将介绍并发的原理。

tornado之异步web服务一的更多相关文章

  1. tornado之异步web服务二

    前面介绍了异步方法带来的显著性能提升.我们来看下异步使用的方法. 1 首先对于get方法使用tornado.web.asynchronous进行装饰.当使用tornado.web.asynchrono ...

  2. 《Introduction to Tornado》中文翻译计划——第五章:异步Web服务

    http://www.pythoner.com/294.html 本文为<Introduction to Tornado>中文翻译,将在https://github.com/alioth3 ...

  3. Python开发【Tornado】:异步Web服务(一)

    异步Web服务 前言: 到目前为止,我们已经看到了许多使Tornado成为一个Web应用强有力框架的功能.它的简单性.易用性和便捷性使其有足够的理由成为许多Web项目的不错的选择.然而,Tornado ...

  4. 如何设计一个异步Web服务——任务调度

    接上一篇<如何设计一个异步Web服务——接口部分> Application已经将任务信息发到了Service服务器中,接下来,Service服务器改如何对自身的资源进行合理分配以满足App ...

  5. 如何设计一个异步Web服务——接口部分

    需求比较简单,提供一个异步Web服务供使用者调用.比如说,某应用程序需要批量地给图片加lomo效果.由于加lomo效果这个操作非常消耗CPU资源,所以我们需要把这个加lomo效果的程序逻辑放到一台单独 ...

  6. Python开发【Tornado】:异步Web服务(二)

    真正的 Tornado 异步非阻塞 前言: 其中 Tornado 的定义是 Web 框架和异步网络库,其中他具备有异步非阻塞能力,能解决他两个框架请求阻塞的问题,在需要并发能力时候就应该使用 Torn ...

  7. 深入理解Tornado——一个异步web服务器

    本人的第一次翻译,转载请注明出处:http://www.cnblogs.com/yiwenshengmei/archive/2011/06/08/understanding_tornado.html原 ...

  8. 第五章:异步Web服务

    到目前为止,我们已经看到了许多使Tornado成为一个Web应用强有力框架的功能.它的简单性.易用性和便捷性使其有足够的理由成为许多Web项目的不错的选择.然而,Tornado受到最多关注的功能是其异 ...

  9. Tornado创建一个web服务

    import tornado.web import tornado.ioloop import tornado.httpserver import tornado.options import con ...

随机推荐

  1. 不同狀況下的 bq25896 register

    no charger時的 bq25896 register [bq25890 reg@] [0x0]=0x7f [0x1]=0x6 [0x2]=0x91 [0x3]=0x1a [0x4]=0x8 [0 ...

  2. Win32 绘制RGB三原色图案

    以前看到三原色的图案,一直很好奇是如何画出来.后来终于搞清楚了,其实很简单,实际上就是RGB三个分量的"位与"运算. 下面给出Win32绘制三原色图案的例子,特此记录在此: #in ...

  3. python 之 线程池实现并发

    使用线程池实现高IO并发 模块:ThreadPoolExecutor, as_completed 测试代码如下: #!/opt/python3/bin/python3 from concurrent. ...

  4. 1005 Spell It Right

    1005 Spell It Right   Given a non-negative integer N, your task is to compute the sum of all the dig ...

  5. xcode5 asset catalogs 由于图标尺寸错误导致编译问题解决[原创]

    如下图,即使图片尺寸不规范,xcode5也可以正常预览(这里我提供的尺寸是57*57, 而需要的是120*120) 但编译运行失败,报的错是: Images.xcassets: error: The ...

  6. Android动画系列 - PropertyAnim 详解

    前言:上一篇文章传统View动画与Property动画基础及比较简单对Android动画系统的基础做了介绍,本篇文章将对PropertyAnimation进行全面深入的探讨,本篇文章可以分为两大块,从 ...

  7. iOS开发 Coretext基本用法

    转至 http://blog.csdn.net/fengsh998/article/details/8691823 API接口文档. https://developer.apple.com/libra ...

  8. mysql序列号发生器

    mysql序列号发生器 学习了:http://www.jquerycn.cn/a_14577 还可以这样啊:

  9. from: 关于RabbitMQ

    from: http://lynnkong.iteye.com/blog/1699684 1      什么是RabbitMQ? RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种, ...

  10. LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

    翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...