Twitter에서 상태를 스트리밍하기 위해 Python과 TwythonStreamer를 사용하고 있습니다. 현재 다음과 같이 수집 된 트윗 수를 세어 자동 연결 해제를 설정했습니다.
먼저 이것을 전역으로 설정했습니다.
max_tweets = 8000
그런 다음에 def on_success(self, tweet)
는 코드 조각이 있습니다.
if (count >= max_tweets):
self.disconnect()
return False
'2014 년 10 월 12 일 00:00:00'과 같이 정확한 날짜와 시간에 스트림 연결을 끊는 방법
스트림 프로세스가 serverForever
서버 처럼 작동하기 때문에 (재시도 횟수를 전달하지 않는 한)
twython/streaming/api.py._request
if self.retry_count and (self.retry_count - retry_counter) > 0:
time.sleep(self.retry_in)
retry_counter += 1
_send(retry_counter)
프로세스를 중지하는 유일한 방법은 일부 조건이 참일 때 콜백 에서 self.shutdown
자체를 수행하는 것입니다. 또는 어려운 방법 : 스트림을 프로세스로 래핑하고 pid 또는 참조를 유지 한 다음 다른 프로세스에서 제거하는 것입니다 ( 여기에 주석 처리됨) . )on_success
편집 : 스 트리머 래퍼 및 호출자의 접근 방식
class Your_Streamer(TwythonStreamer, Process):
def __init__(self, ):
Process.__init__(self)
consumer_key, consumer_secret, oauth_token,oauth_secret = '','','','' #your twitter keys
super(SocialVane_Streamer, self).__init__(consumer_key, consumer_secret, oauth_token,oauth_secret)
def run (self):
keyword_list =[] #the keywords you want to monitorize
if keyword_list:
logger.info("start_monitoring, tracking keywords = %s" % ','.join(keyword_list))
self.statuses.filter(track=','.join(keyword_list))
def on_error(self, status_code, data):
print status_code
# Want to stop trying to get data because of the error?
# Uncomment the next line!
# self.disconnect()
def on_success(self, data):
if 'text' in data:
pass #do what you need
이제 다른 스레드에서 main
메서드 또는 원하는 항목이 될 수 있습니다.
stream = Your_Streamer()
if stream.ready():
stream.start()
logger.debug("pid %i created" % stream.pid)
while not your_stop_condition:
import time
time.sleep(1000) #some sleep
stream.terminate()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다