python 中的 sleep() 是否会阻止访问对象的变量?

发布于 2024-10-09 19:36:50 字数 1056 浏览 0 评论 0原文

我有一个对象要执行一个操作,然后休眠 1 秒,然后再次执行该操作。然而,该对象具有需要随时访问的变量,无论它是否处于一秒睡眠周期。当对象的执行被暂停时,它的变量是否不可访问?

这是Python。

谢谢。

编辑1: 澄清“不可访问”:

对象 A 具有对象 B 需要重复访问的变量 x。 对象 A 休眠 1 秒。如果对象 B 在 A 睡眠时尝试使用 x 会出现问题吗?

编辑2: 忘记提及这两个对象作为单独的进程运行(我使用进程来避免 GIL)

编辑 3:

class QueueController(Process):
    def __init__(self):
        Process.__init__(self)
        self.queue_stream    = Queue()
        self.queue_language  = 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)

使用此 QueueController 类的另一个对象是否仍然能够使用“queues_”变量,即使它休眠一秒钟?我担心睡眠会停止执行,而且还会对这些queue_变量进行访问作为副作用。

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  = 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玻璃人 2024-10-16 19:36:50

time.sleep() 确实释放了 GIL,因此其他线程将被允许运行。

我给出这个答案而不是回答您提出的问题的原因是您提出的问题毫无意义。除了其他线程运行的能力之外,不会阻止任何访问。

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.

别靠近我心 2024-10-16 19:36:50

从来没有接触过Python,但我仍然认为我知道答案。问题是:除了主线程之外还有其他线程吗?如果答案是肯定的,那么它们是可以访问的(注意这里的竞争条件),因为一个线程可以进入睡眠状态,但其他线程仍然处于活动状态。
如果你没有任何其他线程,那么你将无法做太多事情,因为主线程正在休眠,所以不会发生太多事情。

编辑:

对象 A 具有对象 B 需要重复访问的变量 x。对象 A 休眠 1 秒。如果对象 B 在 A 睡眠时尝试使用 x 会出现问题吗?

正如有人已经说过的,对象不会休眠,线程会休眠。因此,如果你有对象 B 在不同的线程中并且 A 进入睡眠状态,你可以从 B 访问 A 所持有的内容,如果没有,主线程(我假设 A 在主线程中运行)将睡眠并且什么也不会发生。如果您对线程是什么有疑问,请检查以下内容:http://en.wikipedia.org/wiki/Thread_%28computer_science%29

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文