python携程gevent_python -gevent协程发http请求-Go语言中文社区

为什么要用协程,python在执行并发任务时有:

多线程用锁,很容易造成数据不稳定,无法使用发挥多核CPU的能力

多进程耗资源

Python通过yield提供了对协程的基本支持,但是不完全。而第三方的gevent为Python提供了比较完善的协程支持。

安装: pip install greenlet#-*- coding: utf-8 -*-

import urllib.request

# url = "http://r.qzone.qq.com/cgi-bin/user/cgi_personal_card?uin=284772894"

url = "http://gc.ditu.aliyun.com/geocoding?a="+urllib.request.quote("苏州市")

HEADER = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','User-Agent':'Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0'}

# 自定义请求的方法,get post

def f(url):

import urllib

import urllib.request

data=urllib.request.urlopen(url).read()

z_data=data.decode('UTF-8')

print(z_data)

# 我自己封装了gevent 的方法,重载了run

from gevent import Greenlet

class MyGreenlet(Greenlet):

def __init__(self, func):

Greenlet.__init__(self)

self.func = func

def _run(self):

# gevent.sleep(self.n)

self.func

count = 3

green_let = []

for i in range(0, count):

green_let.append(MyGreenlet(f(url)))

for j in range(0, count):

green_let[j].start()

for k in range(0, count):

green_let[k].join()

运行结果D:appPython34python.exe D:/app/dgm/field_study.py

{"lon":120.58531,"level":2,"address":"","cityName":"","alevel":4,"lat":31.29888}

{"lon":120.58531,"level":2,"address":"","cityName":"","alevel":4,"lat":31.29888}

{"lon":120.58531,"level":2,"address":"","cityName":"","alevel":4,"lat":31.29888}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值