首页 > 编程语言 > 详细

Python学习---IO的异步[twisted模块]

时间:2018-08-05 11:41:29      阅读:131      评论:0      收藏:0      [点我收藏+]

安装twisted模块

Linux:

  pip3 install twisted

Window:

a. http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted,

下载:Twisted-17.1.0-cp35-cp35m-win_amd64.whl

b. 进入文件所在目录

c.  pip3 install Twisted-17.1.0-cp35-cp35m-win_amd64.whl

Twisted实例[小有问题]

from twisted.web.client import getPage  # 专门用来发送HTTP请求的
from twisted.web.client import defer
from twisted.internet import reactor    # 循环等待用户请求的响应

def all_done(arg):
    reactor.stop()

def callback(contents):
    print(contents)

deferred_list = []

url_list = [‘http://www.bing.com‘, ‘http://www.baidu.com‘, ]
for url in url_list:
    deferred = getPage(bytes(url, encoding=‘utf8‘))  # 加载url交给getPage()
    deferred.addCallback(callback)  # 回调函数表示请求完成后,需要做的操作
    # deferred_list.append(deferred)  #

# dlist = defer.DeferredList(deferred_list)
# dlist.addBoth(all_done)

reactor.run()   # de

Python学习---IO的异步[twisted模块]

原文:https://www.cnblogs.com/ftl1012/p/9424829.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!