请求库的Twisted适配器。
requests-twisted的Python项目详细描述
请求库的扭曲适配器
用于requestshttp库的小插件,以便它可以 通过延迟对线程的阻塞调用,与Twisted一起透明地使用。
它只是将所有请求封装在defertothread中,真的。
用法:
# To use requests with Twisted, you just need to use# a our requests.Session subclass manually.# See http://docs.python-requests.org/en/latest/user/advanced/# for some informations about the Session class.fromrequests_twistedimportTwistedRequestsSessionsession=TwistedRequestsSession()# Then after, instead of doing requests.get|post|whatever(), you just do# session.get|post|whatever().# It returns a defer :defer=session.get('http://github.com/sametmax/')defprint_status(response):print(response.url,response.status_code)# Which you just handle as you would usually do.defer.addCallback(print_status)
请求api没有更改,唯一的区别是延迟。
如果您正在执行很多请求,请记住可以通过执行以下操作来更改reactor线程池:
fromtwisted.internetimportreactorreactor.suggestThreadPoolSize(number_of_threads)
线程池的默认大小取决于正在使用的电抗器;默认电抗器使用的最小大小为5,最大大小为10。在彻底改变线程池大小之前,请注意了解线程及其资源使用情况。
安装
pip install requests-twisted
信息
支持:Python2.7(可能是2.6,我没有测试它) 许可证:zlib