Access global queue object in python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I am building a messaging app in python that interfaces with twitter I want to create a global fifo queue that the twitterConnection object can access to insert new messages.

this is the main section of the app:

    #!/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()

Specifically I want to be able to call the .GetMentions() method of the TwitterConnector class and process any new mentions and put those messages into the queue for processing separately.

This is the twitterConnector class so far:

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  

Any assistance would be greatly appreciated.

Update: let me see if I can clarify the use case a bit. My TwitterConnector is intended to retrieve messages and will eventually transform the twitter status object into a simplified object that contains the needed information for downstream processing. In the same method, it will perform some other calculations to try to determine other needed information. It is actually the transformed objects that I want to put into the queue. hopefully that makes sense. As a simplified example, this method would map the twitter status object to a new object that would contain properties such as: origin network (twitter), username, message, and location if available. This new object would then be put into the queue.

eventually there will be other connectors for other messaging systems that will perform similar actions. They will receive a message populate the new message object and place them into the same queue for further processing. Once the message has been completely processed a response would be formulated and then added to an appropriate queue for transmittal. Using the new object described above the once the actual processing of the tweet content took place then based upon the origin network and username a response might be returned to the originator via twitter.

I had thought of passing a reference to the queue in the contractor (or as an argument to the method) I had also thought of modifying the method to return a list of the new objects and iterate over them to put them into the queue, but I wanted to make sure there wasn t a better or more efficient way to handle this. I also wish to be able to do the same thing with a logging object and a database handler. Thoughts?

Answers

The problem is that "global" should appear on a line of its own

global requestQueue
requestQueue.add(mention)

Moreover, the requestQueue must appear in the module that is defining the class.

If you are importing the requestQueue symbol from another class, you don t need the global at all.

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

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/8352736/access-global-queue-object-in-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils