2011-05-17 19 views
5

का उपयोग करके दिए गए स्ट्रीम उदाहरण पर एक PycURL अनुरोध को रोकने, बंद करने, बंद करने या बंद करने के लिए कैसे करें वर्तमान में मैं ट्विटर एपीआई स्ट्रीम (http://stream.twitter.com/1/statuses/sample.json) को कर्लिंग कर रहा हूं, इसलिए लगातार डेटा प्राप्त कर रहा हूँ। एक बार मैंने ऑब्जेक्ट्स की एक्स संख्या पुनर्प्राप्त करने के बाद स्ट्रीम को कर्लिंग करना बंद करना चाहते हैं (उदाहरण में मैं 10 को मनमानी संख्या के रूप में देता हूं)।ट्विटर स्ट्रीम

आप देख सकते हैं कि मैंने नीचे दिए गए कोड में कनेक्शन को बंद करने का प्रयास कैसे किया है। Curling.perform() के नीचे कोड इस तथ्य के कारण कभी निष्पादित नहीं करता है कि यह डेटा की निरंतर स्ट्रीम है। इसलिए मैंने body_callback में स्ट्रीम को बंद करने का प्रयास किया, हालांकि प्रदर्शन() वर्तमान में चल रहा है, मैं नज़दीक() को आमंत्रित नहीं कर सकता।

किसी भी मदद की सराहना की जाएगी।

कोड:

# Imports 
import pycurl # Used for doing cURL request 
import base64 # Used to encode username and API Key 
import json # Used to break down the json objects 

# Settings to access stream and API 
userName = 'twitter_username' # My username 
password = 'twitter_password' # My API Key 
apiURL = 'http://stream.twitter.com/1/statuses/sample.json' # the twitter api 
tweets = [] # An array of Tweets 

# Methods to do with the tweets array 
def how_many_tweets(): 
    print 'Collected: ',len(tweets) 
    return len(tweets) 

class Tweet: 
    def __init__(self): 
     self.raw = '' 
     self.id = '' 
     self.content = '' 

    def decode_json(self): 
     return True 

    def set_id(self): 
     return True 

    def set_content(self): 
     return True 

    def set_raw(self, data): 
     self.raw = data 

# Class to print out the stream as it comes from the API 
class Stream: 
    def __init__(self): 
     self.tweetBeingRead ='' 

    def body_callback(self, buf): 
     # This gets whole Tweets, and adds them to an array called tweets 
     if(buf.startswith('{"in_reply_to_status_id_str"')): # This is the start of a tweet 
      # Added Tweet to Global Array Tweets 
      print 'Added:' # Priniting output to console 
      print self.tweetBeingRead # Printing output to console 
      theTweetBeingProcessed = Tweet() # Create a new Tweet Object 
      theTweetBeingProcessed.set_raw(self.tweetBeingRead) # Set its raw value to tweetBeingRead 
      tweets.append(theTweetBeingProcessed) # Add it to the global array of tweets 
      # Start processing a new tweet 
      self.tweet = buf # Start a new tweet from scratch 
     else: 
      self.tweetBeingRead = self.tweetBeingRead+buf 
     if(how_many_tweets()>10): 
      try: 
       curling.close() # This is where the problem lays. I want to close the stream 
      except Exception as CurlError: 
       print ' Tried closing stream: ',CurlError 

# Used to initiate the cURLing of the Data Sift streams 
datastream = Stream() 
curling = pycurl.Curl() 
curling.setopt(curling.URL, apiURL) 
curling.setopt(curling.HTTPHEADER, ['Authorization: '+base64.b64encode(userName+":"+password)]) 
curling.setopt(curling.WRITEFUNCTION, datastream.body_callback) 
curling.perform() # This is cURLing starts 
print 'I cant reach here.' 
curling.close() # This never gets called. :(

उत्तर

4

आप एक नंबर है कि एक ही राशि नहीं है के रूप में यह करने के लिए पारित किया गया था वापस लौट कर लिखने कॉलबैक गर्भपात कर सकते हैं। (डिफ़ॉल्ट रूप से यह 'कोई नहीं' वापस लौटाता है जैसा कि उसी संख्या को वापस करने के समान होता है)

जब आप इसे निरस्त करते हैं, तो संपूर्ण स्थानांतरण माना जाएगा और आपका प्रदर्शन() कॉल ठीक से लौटाएगा।

उस स्थानांतरण को तब एक त्रुटि वापस कर दी जाएगी क्योंकि स्थानांतरण रद्द कर दिया गया था।

+0

आपकी मदद डैनियल के लिए धन्यवाद। – jonhurlock

+0

कृपया ध्यान दें कि स्थानांतरण त्रुटि में बंद कर दिया जाएगा (WRITEFUNCTION रिपोर्ट्स ने बाइट्स की सही संख्या नहीं लिखा है)। यदि आपके पास डाउनलोड की सफलता का तर्क परीक्षण है तो यह असफल हो जाएगा। – Fafaman

संबंधित मुद्दे