使用python 发送数据到第三方接口,同步等待太慢

使用python 发送数据到第三方接口,同步等待太慢 这里导致无法快速的推送数据到第三方,第三方可能需要5秒-10秒才会返回结果,所有这里采用了异步发生的方式,不等待第三方的HTTP状态,采用异步方式。源代码如下:

python 复制代码
pip install requests-futures


from requests_futures.sessions import FuturesSession

# 初始化异步请求池
self.sreq = FuturesSession(max_workers=10)



# 原 send 位置改成:
future = self.sreq.post('http://127.0.0.1/send_data',
    data=json.dumps(row),
    headers=headers,
    timeout=0.2
)

future.add_done_callback(self.handle_response)


#回调方法
def handle_response(self, resp_future):
    try:
        response = resp_future.result()  # 拿结果(不阻塞主线程)
        if response.status_code == 200:
            logging.info('save_face成功')
        else:
            logging.error('save_face失败 code=%s text=%s', response.status_code, response.text)
    except Exception as e:
        logging.error('异步HTTP异常: %s', e)
相关推荐
星云穿梭18 小时前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵18 小时前
用 Pygame 实现 15 puzzle
python·数学·游戏
黄忠1 天前
大模型之LangGraph技术体系
python·llm
hboot2 天前
AI工程师第二课 - 数据处理
人工智能·python·数据分析
用户8356290780512 天前
使用 Python 自动化 PowerPoint 形状布局与格式设置
后端·python
用户8356290780512 天前
用 Python 自动化 PowerPoint 演讲者备注添加
后端·python
黄忠2 天前
01-系统架构设计-LangGraph状态机与多源异构RAG
python
zzzzzz3102 天前
假如我是掘金管理员,我先给评论区装个'代码审查'系统
python·程序员·机器人
砍材农夫2 天前
python环境|conda安装和使用(2)
后端·python
程序员龙叔3 天前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试