ストリーム API を監視するために、竜巻ベースの Twitter ストリーミング モジュールである TweetStream (https://github.com/joshmarshall/TweetStream) を使用しています。
追跡された単語を変更したい場合、取得プロセスを再開する方法を知りたいです。
私の現在の解決策(正確には解決策ではありません)は、いくつかのエラーを出しています。
stream = tweetstream.TweetStream(configuration,ioloop=main_io_loop)
stream.fetch("/1.1/statuses/filter.json?track="+tornado.escape.url_escape(words), callback=callback)
def check_words():
global words
with open('words.txt') as file:
newwords = file.read()
if words != newwords:
words = newwords
try:
print newwords
stream.fetch("/1.1/statuses/filter.json?track="+tornado.escape.url_escape(words), callback=callback)
except:
pass
file.close()
interval_ms = 1000*10
scheduler = tornado.ioloop.PeriodicCallback(check_words,interval_ms,io_loop = main_io_loop)
scheduler.start()
main_io_loop.start()
これが私が得ているエラーです
ERROR:root:Uncaught exception, closing connection.
Traceback (most recent call last):
File "/home/user/PycharmProjects/observrenv/local/lib/python2.7/site-packages/tornado/iostream.py", line 305, in wrapper
callback(*args)
File "/home/user/PycharmProjects/observrenv/src/tweetstream/tweetstream.py", line 155, in on_connect
self._twitter_stream.read_until("\r\n\r\n", self.on_headers)
File "/home/user/PycharmProjects/observrenv/local/lib/python2.7/site-packages/tornado/iostream.py", line 151, in read_until
self._set_read_callback(callback)
File "/home/user/PycharmProjects/observrenv/local/lib/python2.7/site-packages/tornado/iostream.py", line 369, in _set_read_callback
assert not self._read_callback, "Already reading"
AssertionError: Already reading
ERROR:root:Exception in callback <tornado.stack_context._StackContextWrapper object at 0x2415cb0>
Traceback (most recent call last):
File "/home/user/PycharmProjects/observrenv/local/lib/python2.7/site-packages/tornado/ioloop.py", line 421, in _run_callback
callback()
File "/home/user/PycharmProjects/observrenv/local/lib/python2.7/site-packages/tornado/iostream.py", line 305, in wrapper
callback(*args)
File "/home/user/PycharmProjects/observrenv/src/tweetstream/tweetstream.py", line 155, in on_connect
self._twitter_stream.read_until("\r\n\r\n", self.on_headers)
File "/home/user/PycharmProjects/observrenv/local/lib/python2.7/site-packages/tornado/iostream.py", line 151, in read_until
self._set_read_callback(callback)
File "/home/user/PycharmProjects/observrenv/local/lib/python2.7/site-packages/tornado/iostream.py", line 369, in _set_read_callback
assert not self._read_callback, "Already reading"
AssertionError: Already reading
check_words を呼び出すときに ioloop を再度開始することで、より良い結果が得られました (最高ではありません)。
stream = tweetstream.TweetStream(configuration,ioloop=main_io_loop)
stream.fetch("/1.1/statuses/filter.json?track="+tornado.escape.url_escape(words), callback=callback)
def check_words():
global words, stream
with open('words.txt') as file:
newwords = file.read()
if words != newwords:
words = newwords
print newwords
try:
stream = tweetstream.TweetStream(configuration,ioloop=main_io_loop)
stream.fetch("/1.1/statuses/filter.json?track="+tornado.escape.url_escape(words), callback=callback)
interval_ms = 1000*10
scheduler = tornado.ioloop.PeriodicCallback(check_words,interval_ms,io_loop = main_io_loop)
scheduler.start()
main_io_loop.start()
except:
pass
file.close()
interval_ms = 1000*10
scheduler = tornado.ioloop.PeriodicCallback(check_words,interval_ms,io_loop = main_io_loop)
scheduler.start()
main_io_loop.start()