登录  /  注册
python - Requests 如何中断请求?
PHPz
PHPz 2017-05-18 10:53:01
[Python讨论组]

python中的requests如何中断请求呢? 我是多线程并发去get,但是没找到停止请求操作,只能wait线程结束,我以前用过socket套接字,里面写个状态停止read那种就可以。 requests没找到类似的方法。

import requests
from threading import Thread
from contextlib import closing

import json
import time


class TestT(Thread):

    def __init__(self):
        super(TestT, self).__init__()

        self.s = requests.session()

    def stop(self):
        self.p.connection.close()
        self.s.close()

    def run(self):
        t = time.time()

        self.p = self.s.get('http://api2.qingmo.com/api/column/tree/one?Pid=8&Child=1', stream=True, timeout=10)

        # 消耗了很多时间
        print time.time()-t

        with closing(self.p) as r:
            print time.time()-t

            data = ''

            for chunk in r.iter_content(4096):
                data += chunk

            print json.loads(data)

        print time.time()-t


t = TestT()
t.start()
t.join(30)
t.stop()
t.join()

改了下,用了流式读取,但是 get的时候,还是花了3秒多,如何中断这3秒?

PHPz
PHPz

学习是最好的投资!

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号