Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的访问全局队列对象_Python_Global Variables - Fatal编程技术网

python中的访问全局队列对象

python中的访问全局队列对象,python,global-variables,Python,Global Variables,我正在用python构建一个与twitter交互的消息传递应用程序。我想创建一个twitterConnection对象可以访问的全局fifo队列,以插入新消息 这是应用程序的主要部分: #!/usr/bin/python from twitterConnector import TwitterConnector from Queue import deque #instantiate the request queue requestQueue = d

我正在用python构建一个与twitter交互的消息传递应用程序。我想创建一个twitterConnection对象可以访问的全局fifo队列,以插入新消息

这是应用程序的主要部分:

    #!/usr/bin/python

    from twitterConnector import TwitterConnector
    from Queue import deque

    #instantiate the request queue
    requestQueue = deque()

    #instantiate the twitter connector
    twitterInterface = TwitterConnector()

    #Get all new mentions
    twitterInterface.GetMentions()
具体来说,我希望能够调用TwitterConnector类的.getAttentions()方法,处理任何新的提及,并将这些消息放入队列中单独处理

这是目前为止的twitterConnector类:

class TwitterConnector:
   def __init__(self):
      self.connection = twitter.Api(consumer_key=self.consumer_key,consumer_secret=self.consumer_secret,access_token_key=self.access_token_key,access_token_secret=self.access_token_secret)
      self.mentions_since_id = None

   def VerifyCredentials(self):
      print self.connection.VerifyCredentials()

   def GetMentions(self):
      mentions = self.connection.GetMentions(since_id=self.mentions_since_id)
      for mention in mentions:
         print mention.text
         global requestQueue.add(mention)  # <- error  
class twitter连接器:
定义初始化(自):
self.connection=twitter.Api(consumer\u key=self.consumer\u key,consumer\u secret=self.consumer\u secret,access\u token\u key=self.access\u token\u key,access\u secret=self.access\u token\u secret)
self.indications\u,因为\u id=None
def验证凭证(自我):
打印self.connection.VerifyCredentials()
def getnotices(self):
提及=self.connection.getAttentions(自\u id=self.attentions\u自\u id)
如有提及:
打印提纲.text
全局请求队列。添加(提及)#问题是“全局”应该出现在它自己的一行上

global requestQueue
requestQueue.add(mention)
此外,requestQueue必须出现在定义类的模块中

如果要从另一个类导入requestQueue符号,则根本不需要全局

from some_module import requestQueue
class A(object):
    def foo(o):
         requestQueue.add(o) # Should work

一般来说,避免使用全球语言是个好主意;更好的设计往往存在。有关全局变量问题的详细信息,请参见例如(,)

如果通过使用globals,您希望在多个
TwitterConnector
实例之间共享单个
requestQueue
,您还可以将队列传递给其构造函数中的连接器:

class TwitterConnector:
   def __init__(self, requestQueue):
      self.requestQueue = requestQueue
      ...

   def GetMentions(self):
      mentions = self.connection.GetMentions(since_id = self.mentions_since_id)
      requestQueue = deque()
      for mention in mentions:
         print mention.text
         self.requestQueue.add(mention)
相应地,您需要向构造函数提供
requestQueue
,如下所示:

twitterInterface = TwitterConnector(requestQueue)

您得到的错误是什么?我要问您一个显而易见的问题:为什么您要定义自己的
TwitterConnector
,而不使用现有的Python API?对于健壮的共享队列,您可能需要使用AMQP。看看芹菜。我建议使用“add_to_queue”方法和接收队列对象的构造函数使所有这些队列输入点从同一基类派生。我同意不使用全局变量,但单例仍然是一种非常有效的模式。只需要在不同的模块上定义,这样就可以将它们作为符号导入。您假设对GetNitensions的每个调用都可以返回不同的队列,而不需要随着时间的推移进行聚合。这是一个主要的功能性差异,他试图做一个有效的点。重构。我也同意Singleton没有错,但我不认为在这里使用这种模式有什么意义(为什么我们要让
twitterConnector
依赖于使用它的特定应用程序?)。如果这不是twitterConnector不可分割的一部分,那么可能正确的做法是将它作为参数传递给GetNinters方法。但实际上,我们需要更深入地理解目的是什么。他已经有了一个用于连接的API,所以也许TwitterConnecter只是一个坏名字。我完全同意你的看法。有了这些信息,我们只能向他提供一些黑客。在句法层面上,这是一个有效的解决方案,但它不能解决设计问题。我的哲学是首先回答这个人,然后告诉他为什么这个问题是错误的。。。