• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python生成器/yield协程/gevent写简单的图片下载器功能示例

python 搞代码 4年前 (2022-01-07) 27次浏览 已收录 0个评论

这篇文章主要介绍了python生成器/yield协程/gevent写简单的图片下载器功能,结合实例形式分析了python生成器、yield协程与gevent图片下载器相关功能定义与使用技巧,需要的朋友可以参考下

本文实例讲述了python生成器/yield协程/gevent写简单的图片下载器功能。分享给大家供大家参考,具体如下:

1、生成器:

 '''第二种生成器''' # 函数只有有yield存在就是生成器 def test(i): while True: i += 1 res = yield i print(res) i += 1 return res def main(): t = test(1) # 创建生成器对象 print(next(t)) # next第一次执行从上到下,yield是终点 print(next(t)) print(t.send(5)) if __name__ == '__main__': main() 

运行结果:

2
None
4
5
6

2、yield协程demo:

 def run1(): while True: print('run1____') yield def run2(): while True: print('run2____') yield def main(): while True: next(run1()) next(run2()) if __name__ == '__main__': main() 

3、gevent写简单的下载图片

 import gevent import requests,lxml # from gevent import monkey # monkey.patch_all() def get_pic(url, list): headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' } response = requests.get(url, headers=headers) with open('./pic/'+str(list.pop(0)) + '.png-600', 'wb') as f: f.write(response.content) def get_pic_name_list(): <p style="color:transparent">来源gao!%daima.com搞$代*!码$网</p>def main(): get_pic_name_list() list = [x for x in range(9999)] gevent.joinall([ gevent.spawn(get_pic, 'http://pic8.iqiyipic.com/image/20181008/eb/af/v_116880780_m_601_m11_180_236.jpg-600', list), gevent.spawn(get_pic, 'http://pic6.iqiyipic.com/image/20181004/a2/2b/v_112874372_m_601_m15_180_236.jpg-600', list) ]) if __name__ == '__main__': main() 

更多关于Python相关内容可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

以上就是python生成器/yield协程/gevent写简单的图片下载器功能示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python生成器/yield协程/gevent写简单的图片下载器功能示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址