的Python Tweepy使用Python 2.7的多任务处理

的Python Tweepy使用Python 2.7的多任务处理

问题描述:

流,我成功地使用下面的代码,听取他们对帐户的直接消息流:的Python Tweepy使用Python 2.7的多任务处理

from tweepy import Stream 
from tweepy import OAuthHandler 
from tweepy import API 
from tweepy.streaming import StreamListener 

# These values are appropriately filled in the code 

consumer_key = '######' 
consumer_secret = '######' 
access_token = '######' 
access_token_secret = '######' 

class StdOutListener(StreamListener): 

    def __init__(self): 
     self.tweetCount = 0 

    def on_connect(self): 
     print("Connection established!!") 

    def on_disconnect(self, notice): 
     print("Connection lost!! : ", notice) 

    def on_data(self, status): 
     print("Entered on_data()") 
     print(status, flush = True) 
     return True 
     # I can add code here to execute when a message is received, such as slicing the message and activating something else 

    def on_direct_message(self, status): 
     print("Entered on_direct_message()") 
     try: 
      print(status, flush = True) 
      return True 
     except BaseException as e: 
      print("Failed on_direct_message()", str(e)) 

    def on_error(self, status): 
     print(status) 

def main(): 

    try: 
     auth = OAuthHandler(consumer_key, consumer_secret) 
     auth.secure = True 
     auth.set_access_token(access_token, access_token_secret) 

     api = API(auth) 

     # If the authentication was successful, you should 
     # see the name of the account print out 
     print(api.me().name) 

     stream = Stream(auth, StdOutListener()) 

     stream.userstream() 

    except BaseException as e: 
     print("Error in main()", e) 

if __name__ == '__main__': 
    main() 

这是伟大的,当我收到我还可以执行代码一条消息,但是我添加到工作队列中的工作需要能够在一段时间后停止。我使用一个流行的start = time.time()并减去当前时间来确定流逝的时间,但是这个流式代码不会循环来检查时间。我只是在等待一个新的消息,所以时钟从不检查这么说。

我的问题是这样的:我怎样才能让流发生,并仍然追踪时间流逝?我是否需要使用本文中描述的多线程? http://www.tutorialspoint.com/python/python_multithreading.htm

我是新来的Python和玩附近的树莓派硬件乐趣。我已经从Stackoverflow学到了很多东西,谢谢大家:)

我不确定你要如何决定什么时候停止,但你可以通过timeout argument到流中放弃一定的延迟。

stream = Stream(auth, StdOutListener(), timeout=30) 

这会调用您的听众的on_timeout() method。如果你返回true,它将继续流式传输。否则,它将停止。

在流的超时参数和您的监听器on_timeout()之间,您应该能够决定何时停止流式传输。

+0

我喜欢你的想法,我想我会把它放在我的后面的口袋里,并尝试这个多线程的东西。我认为,如果我可以将流分成单个函数(我认为它将是main()),并且它可以更新全局变量,然后可以由其他线程访问,但我可能会很乐意去。 – sakko303

我发现我能够以我想要的方式获得一些多线程代码。我给出了一个例子,用不同的定时参数启动相同代码的多个实例,我可以得到两个不同的代码块,以在它们自己的实例中运行。

一个代码块不断向全局变量中添加10 VAR)。 另一个块会在5秒钟后检查,然后打印var的值。

这演示了使用Python多线程执行和共享数据的2个不同任务。

见下面的代码

import threading 
import time 

exitFlag = 0 
var = 10 

class myThread1 (threading.Thread): 
    def __init__(self, threadID, name, counter): 
     threading.Thread.__init__(self) 
     self.threadID = threadID 
     self.name = name 
     self.counter = counter 
    def run(self): 
     #var counting block begins here 
     print "addemup starting" 
     global var 
     while (var < 100000): 
      if var > 90000: 
       var = 0 
      var = var + 10 



class myThread2 (threading.Thread): 
    def __init__(self, threadID, name, counter): 
     threading.Thread.__init__(self) 
     self.threadID = threadID 
     self.name = name 
     self.counter = counter 
    def run(self): 
     #time checking block begins here and prints var every 5 secs 
     print "checkem starting" 
     global var 
     start = time.time() 
     elapsed = time.time() - start 
     while (elapsed < 10): 
      elapsed = time.time() - start 
      if elapsed > 5: 
       print "var = ", var 
       start = time.time() 
       elapsed = time.time() - start 

# Create new threads 
thread1 = myThread1(1, "Thread-1", 1) 
thread2 = myThread2(2, "Thread-2", 2) 

# Start new Threads 
thread1.start() 
thread2.start() 

print "Exiting Main Thread" 

我的下一个任务将是打破我的Twitter流在其自己的线程,并通过作为变量排队程序的任务直接接到消息,而希望第一个线程继续听更直接的信息。

+0

您链接的教程太可怕了。最后提供使用Python的Queue,并在其上添加同步机制。我错过了什么吗?为什么这需要?此外,它称之为“优先队列” - 它不是(再次,据我所知 - 我错过了什么?) – guyarad