2012-08-14 270 views
4

我应该在哪里在pycurl持续性连接检查断开?检测断开持久卷曲连接

某处在我的剧本的连接垂死/超时/抛出一个错误,但脚本保持打开状态。我需要检测问题,以便我可以重新启动脚本。

我们连接到GNIP(社交媒体数据提供商)

我的代码是在这里:https://gist.github.com/3353033

我读过了与libcurl的选项,我通过PHP curl_setopts文档阅读,因为他们也利用libcurl。

class Client: 
    time_start = time.time() 
    content = "" 
    def __init__(self,options): 
     self.options = options 
     self.buffer = "" 
     self.conn = pycurl.Curl() 
     self.conn.setopt(pycurl.USERPWD, "%s:%s" % (USER, PASS)) 
     self.conn.setopt(pycurl.ENCODING,'gzip') 
     self.conn.setopt(pycurl.URL, STREAM_URL) 
     self.conn.setopt(pycurl.WRITEFUNCTION, self.on_receive) 
     self.conn.setopt(pycurl.FOLLOWLOCATION,1) 
     self.conn.setopt(pycurl.MAXREDIRS, 5) 
     self.conn.setopt(pycurl.COOKIEFILE,"cookie.txt") 
     try: 
      self.conn.perform() 
     except Exception,e: 
      print e.message 

    def on_receive(self, data): 
     self.buffer += data 

     if data.endswith("\r\n") and self.buffer.strip(): 
      if(self.triggered()): 
       if(len(self.buffer) != 0): 
        try: 
         SaveThread(self.buffer).start() 
        except Exception, e: 
         print "something i commented would have told you there was an error" 
         system.exit(1) 
       self.buffer = "" 

    def triggered(self): 
     # First trigger based on size then based on time.. 
     if (len(self.buffer) > SAVE_FILE_LENGTH): 
      return True 
     time_end = time.time() 
     if (((time_end - self.time_start) > ROLL_DURATION)): #for the time frame 
      self.time_start=time.time() 
      return True 
     return False 

编辑:我已经固定要点

+0

你的要点是不正确缩进。我也建议你在这里发布你的源代码。 – stderr 2012-08-14 21:14:18

+0

@MikeSteder我相信我已经固定的要点,我已经在这里复制它。谢谢! – Jake 2012-08-14 21:29:10

回答

0

在上面的代码system.exit(1)应该是sys.exit(1)吧?

除此之外,你还有没有更多的except子句可能会引起sys.exit(1)引发的SystemExit异常?

+0

我不看此刻的剧本,但我相信,这不是问题。我们其实已经想出了一个解决方案,并计划在第二天左右将它作为答案发布。问题的要点是网络连接问题是一个问题。如果该行被丢弃那么我们的供应商认为它应该仍然可以发送数据沿管路并断开从缓冲区写入错误的结束,而在我们这边,我们从一个空缓冲区中读取,不知道该连接是死。我们使用了一些低数据速度/超时选项来克服这一点,并在低数据时断开连接。 – Jake 2012-08-16 06:15:51

+0

嗨,我们与Gnip有类似的问题。您能否详细说明您的解决方案?我们的代码看起来与您的代码非常相似。你如何检测低数据速度? – 2012-10-10 21:08:09

+0

@DeepKapadia设置LOW_SPEED_LIMIT和LOW_SPEED_TIME选项,当网络流量低于这些限制时,您将收到一个错误(28) (请参阅:http://curl.haxx.se/libcurl/c/CURLOPT_LOW_SPEED_LIMIT.html) – nickmilon 2015-08-12 01:47:34