You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tweepy中同时使用on_data与on_response时后者失效的原因咨询

问题原因及解决办法

核心原因

Tweepy的StreamingClient中,默认的on_data方法负责把原始数据解析为Response对象,再触发on_response方法。当你自行重写on_data但未调用父类的on_data实现时,on_response的触发逻辑会被中断,导致它无法运行。

解决方法

在你重写的on_data方法末尾,调用父类的on_data方法,即可让on_response正常执行:

class MyStream(tweepy.StreamingClient):
   def on_data(self, raw_data):
      print(raw_data)
      # 调用父类on_data,触发on_response逻辑
      return super().on_data(raw_data)
    
   def on_response(self, response):
      print(response)  # 现在可以正常运行

补充说明

如果不需要在on_data中做额外处理,直接删除自己重写的on_data方法即可,此时父类默认的on_data会自动触发on_response。

内容的提问来源于stack exchange,提问作者random debagger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 16:10:33