Python使用Twisted总结 缓慢的诗

这个标题容易让人产生疑惑,至少我在看Twsited系列教程的时候,第一眼看见确认让人费劲,不过相信你把这篇文章读完就明白是什么含义了。

    希望读者具有一定的socket基础,如果没有接触过,请参考socket document手册。你也可以在public git repository下载源代码参考,或是使用git或其他的版本管理软件clone出一份代码,如下:

 
 
  1. git clone git://github.com/jdavisp3/twisted-intro.git 

    如果你比较怕麻烦,可以在LinuxIDC.com进行下载,下载地址:

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/5月/26日/Python使用Twisted总结/

    下载完之后,twisted-info中有一个slow server的demo,大家可以用如下方式运行下,看下效果——缓慢的诗。

 
 
  1. [root@localhost twisted-intro]# python blocking-server/slowpoetry.py --num-bytes 50 --delay 5 --port 51296 poetry/ecstasy.txt  
  2.  
  3. [root@localhost twisted-intro]# nc localhost 51296 

    如果一切正常的话,你会看到一条小诗被每次输出一些字节,一旦这首小诗被完全生产出来,服务器会断开连接。

    除此之外,你可以尝试再打开一个新的Client去连接Server,新打开的Client必须等待第一个Client被处理完才会接收信息,的确是一首缓慢的诗^_^

    在上面的例子中,客户端是阻塞的,现在我们分别运行三个不同任务的客户端,去连接三个不同的服务器,看下效果,如下:

 
 
  1. [root@localhost twisted-intro]# python blocking-server/slowpoetry.py --port 10000 poetry/ecstasy.txt --num-bytes 30  
  2. Serving poetry/ecstasy.txt on port 10000.  
  3.  
  4. [root@localhost twisted-intro]# python blocking-server/slowpoetry.py --port 10001 poetry/fascination.txt  
  5. Serving poetry/fascination.txt on port 10001.  
  6.  
  7. [root@localhost twisted-intro]# python blocking-server/slowpoetry.py --port 10002 poetry/science.txt  
  8. Serving poetry/science.txt on port 10002.  
  9.  
  10. [root@localhost twisted-intro]# python blocking-client/get-poetry.py 10000 10001 10002  
  11. Task 1: get poetry from: 127.0.0.1:10000  
  12. Task 1: got 3003 bytes of poetry from 127.0.0.1:10000 in 0:00:10.122140  
  13. Task 2: get poetry from: 127.0.0.1:10001  
  14. Task 2: got 615 bytes of poetry from 127.0.0.1:10001 in 0:00:06.212043  
  15. Task 3: get poetry from: 127.0.0.1:10002  
  16. Task 3: got 653 bytes of poetry from 127.0.0.1:10002 in 0:00:06.613744  
  17. Got 3 poems in 0:00:22.947927 

     注意观察,以上任务是一个一个顺序运行的,至于产生阻塞的原因也很简单,主要在for循环遍历的时候,是一个任务一个任务顺序遍历的。

 
 
  1. Task 1: got 30 bytes of poetry from 127.0.0.1:10000  
  2. Task 2: got 10 bytes of poetry from 127.0.0.1:10001  
  3. Task 3: got 10 bytes of poetry from 127.0.0.1:10002  
  4. Task 1: got 30 bytes of poetry from 127.0.0.1:10000  
  5. Task 2: got 10 bytes of poetry from 127.0.0.1:10001  
  6. Task 3: got 10 bytes of poetry from 127.0.0.1:10002  
  7. ……  
  8. Task 2 finished  
  9. Task 3: got 10 bytes of poetry from 127.0.0.1:10002  
  10. Task 1: got 30 bytes of poetry from 127.0.0.1:10000  
  11. Task 3: got 10 bytes of poetry from 127.0.0.1:10002  
  12. Task 1: got 30 bytes of poetry from 127.0.0.1:10000  
  13. ……  
  14. Task 3 finished  
  15. Task 1: got 30 bytes of poetry from 127.0.0.1:10000  
  16. Task 1: got 30 bytes of poetry from 127.0.0.1:10000  
  17. ……  
  18. Task 1 finished  
  19. Task 1: 3003 bytes of poetry  
  20. Task 2: 615 bytes of poetry  
  21. Task 3: 653 bytes of poetry  
  22. Got 3 poems in 0:00:10.118896 

    注意这次任务执行的顺序,是交替的。从时间的角度来讲,异步的时间比同步的时间快很多。虽然异步的客户端也存在I/O阻塞情况,但通过交替执行可以减少阻塞时间,从而加快运行时间。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值