我有一个对象是执行一个动作,然后在再次执行动作之前休眠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)
Run Code Online (Sandbox Code Playgroud)
使用此QueueController类的另一个对象是否仍然能够使用'queues_'变量,即使它睡眠一秒钟?我担心睡眠会停止执行,但也会将这些queue_变量作为副作用进行访问.
Ign*_*ams 11
time.sleep() 确实释放了GIL,因此允许其他线程运行.
我给出这个答案而不是回答你问过的问题的原因是你提出的问题毫无意义.除了其他线程运行的能力之外,不会阻止任何访问.