Python线程锁定所有程序

时间:2016-07-04 23:16:37

标签: python multithreading tweepy

我使用twitter Api tweepy和python 2.7.11创建了一个StreamListener。

我在类的构造函数中创建一个线程:

 def __init__(self, api=None, **options):

    StreamListener.__init__(self,api)

    self.queue = Queue(maxsize = options.get("queue_size", 100) )
    self.queue_thread = Thread(target=self._listen())
    self.tweet_list = OrganizedList(options.get("list_size", 50))

    #And more code...

但是主程序的执行会改变为线程函数,而不是继续执行init函数。我尝试使用daemon = True创建线程,但它不起作用。

完整代码位于https://github.com/pablomm/tweetbot/blob/master/tweetbot/utils.py

如何让线程在后台运行呢?

[编辑]解决了,正确的代码是

def __init__(self, api=None, **options):

    StreamListener.__init__(self,api)

    self.queue = Queue(maxsize = options.get("queue_size", 100) )
    self.queue_thread = Thread(target=self._listen) #<-- without ()
    self.tweet_list = OrganizedList(options.get("list_size", 50))

0 个答案:

没有答案