开发者

Does sleep() in python prevent access to an object's variables?

I have an object that is to perform an action, and then sleep for 1 second before performing the action again. However, the object has variables that need to be accessible at all times regardless if it is in its one second sleep period. Is when the object's execution is suspended, are its variables inaccessible?

This is Python.

Thanks.

EDIT 1: Clarifying 'inaccessible':

Object A has variable x that Object B needs to access repeatedly. Object A sleeps for 1 second. Will there be a problem if Object B tries to use x while A is sleeping?

EDIT 2: Forgot to mention the two objects are running as individual processes (I'm using processes to avoid the GIL)

EDIT 3:

class QueueController(Process):
    def __init__(self):
        Process.__init__(self)
        self.queue_stream    = Queue()
        self.queue_language  开发者_Go百科= Queue()
        self.queue_expander  = Queue()
        self.queue_feature   = Queue()
        self.queue_classify  = Queue()
        self.db = connect_to_db()

    def run(self):
        # Every second, allow 10 more tweets from the raw_tweets db
        # enter the queue_stream to be processed
        value = 0
        while True:
            for i in db.raw_tweets.find().skip(value).limit(30):
                self.queue_stream.put(i)
                value+=30
                sleep(1)

Will another object who uses this QueueController class still be able to use the 'queues_' variables even when it sleeps for one second? I'm afraid that sleeping stops execution, but also access to those queue_ variables as a side affect.


time.sleep() does release the GIL, so other threads will be allowed to run.

The reason I give this answer instead of answering the question you've asked is that the question you've asked makes no sense. No access is ever prevented other than the ability for other threads to run.


Never touched much on python but still I think I do know the answer. The thing is: there are any threads other than main? If the answer is yes, then they are accessible (watch out for racing conditions here), since a thread can go to sleep but the others are still active. If you don't have any other threads, then you won't be able to do much, since the main thread is sleeping, so nothing much will happen.

EDIT:

Object A has variable x that Object B needs to access repeatedly. Object A sleeps for 1 second. Will there be a problem if Object B tries to use x while A is sleeping?

As someone told already, objects don't go to sleep, threads do. So, if you have object B in a different thread and A go to sleep, you can access from B what A holds, if not, the main thread ( I assume A is running in the main thread) will sleep and nothing happens. If you have doubts of what a thread is, check this:http://en.wikipedia.org/wiki/Thread_%28computer_science%29

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜