python 2.7 requests_python 2.7 如何实现http post多并发?

注意是python 2.7, (multiprocessing值支持到2.6,2.7下安装会报错)

用threadpool试了一下,总是报错:

单次并发的函数

data = ['a', 'b', 'c']

#data传到threadpool.makeRequests里面的第二个参数,函数会将这个列表里面的值取出来,然后根据并发数进行并发。

def repost(data):

response = requests.post(url, data).text

return response

结果处理函数,只是打印出来

def postresult(requestself, responseresult):

print(responseresult)

#其他的处理,比如对结果插入到数据库,或者写入日志等,可以在这里写

pool = threadpool.ThreadPool(5)

requests = threadpool.makeRequests(repost, data, postresult)

**#之前出问题,是因为这里requests的名称定义和requests()方法冲突了。。。然后报错又很奇怪,没引起注意,把这里的requests改个名字就OK了。各位,承让了。

当然,下面这一样,也改一下。requests的名称。

[pool.putRequest(req) for req in requests]

pool.wait()

报错如下:

Traceback (most recent call last):

File "C:\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\threadpool.py", line 158, in run

result = request.callable(*request.args, **request.kwds)

File "C:\test.py", line 29, in repost

response = requests.post(url, data1).text

AttributeError: 'list' object has no attribute 'post'

或者有没有其他实现方法?

我靠,我靠,我靠靠靠。。。。问题解决了。看上面的备注。再见。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值