我已经解决了这个问题,但我想知道我做错了什么。我创建了一个 Queue.Queue(( 并错误地调用了 get(timeout=60(。队列为空,因此呼叫被阻止。我用 CTRL-C 打断了它。我实例化了一个辅助线程。在睡眠后将填充队列的线程。当辅助线程处于睡眠状态时,我调用了 get(( 它再次阻塞。当辅助线程唤醒时,它填充了队列,但主 get(( 没有返回。为什么?
该文件 secondary.py:
import time, threading
class putter(threading.Thread):
def __init__(self, q):
super(putter, self).__init__()
self.q = q
self._keep_going = True
def stop(self):
self._keep_going = False
def run(self):
print('T2 sleeping to allow calling get')
time.sleep(5)
print('T2 awoke, putting')
self.q.put('Hello world!')
while self._keep_going:
time.sleep(1)
print('{} T2 running'.format(time.time()))
print('qsize {}'.format(self.q.qsize()))
互动口译环节:
bruno@jeeg:/mnt/storage/src/foo$ python
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Queue
>>> from secondary import putter
>>> queue = Queue.Queue()
>>> queue.get(timeout=60)
^CTraceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/Queue.py", line 177, in get
self.not_empty.wait(remaining)
File "/usr/lib/python2.7/threading.py", line 359, in wait
_sleep(delay)
KeyboardInterrupt
>>> p = putter(queue)
>>> p.start()
T2 sleeping to allow calling get
>>> queue.get()
T2 awoke, putting
1491384824.07 T2 running
qsize 1
1491384825.08 T2 running
qsize 1
1491384826.08 T2 running
qsize 1
1491384827.08 T2 running
[... forever]
另一个会话,测试@Himal
>>> p.start()
T2 sleeping to allow calling get
>>> queue.get(timeout=4)
T2 awoke, putting
1491401131.61 T2 running
qsize 1
1491401132.61 T2 running
qsize 1
'Hello world!'
>>> 1491401133.61 T2 running
qsize 0
1491401134.61 T2 running
qsize 0
[... T2 keeps running but now get(timeout=4) has returned and the queue is empty]
对get()
的调用返回,但交互式口译会话没有。后者的原因很可能是另一个线程仍在运行,但我不是 100% 确定。
尝试运行此程序:
import time, threading
class putter(threading.Thread):
def __init__(self, q):
super(putter, self).__init__()
self.q = q
self._keep_going = True
def stop(self):
self._keep_going = False
def run(self):
print('T2 sleeping to allow calling get')
time.sleep(5)
print('T2 awoke, putting')
self.q.put('Hello world!')
while self._keep_going:
time.sleep(1)
print('{} T2 running'.format(time.time()))
print('qsize {}'.format(self.q.qsize()))
from Queue import Queue
q = Queue()
p = putter(q)
p.start()
q.get()
print("Get returned!")
这将正确打印以下输出:
T2 sleeping to allow calling get
T2 awoke, putting
Get returned!
1491386038.93 T2 running
qsize 0
1491386039.93 T2 running
qsize 0
1491386040.94 T2 running
qsize 0
...
当我在交互式解释器中运行以下代码时,输出符合预期,命令甚至返回(请参阅Get returned!
后行中的>>>
(。后者可能与您的结果不同,因为我们使用的是不同的操作系统。
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from so_test import putter
>>> from Queue import Queue
>>> q = Queue()
>>> putter(q).start()
T2 sleeping to allow calling get
>>> q.get(); print("Get returned!")
T2 awoke, putting
'Hello world!'
Get returned!
>>> 1491387024.89 T2 running
qsize 0
1491387025.89 T2 running
qsize 0
1491387026.89 T2 running
qsize 0
1491387027.89 T2 running
qsize 0
1491387028.9 T2 running
...