在书中看到这个例子,引入 resume 是为了可以在网络中断或访问目标中断时恢复会话。
正常状况下, resume 保持为空,继续队列的 put ;
resume 不为空时,恢复队列。
问题是,它是怎么控制 resume 的赋值的?这点想不通。代码里只有这个函数跟 resume 有关,其它都不涉及。
```
import Queue
resume = None
def build():
with open('123.txt', 'r') as f:
raw_words = f.readlines()
found_resume = False words = Queue.Queue() for word in raw_words: word = word.strip() if resume is not None: if found_resume: words.put(word) else: if word == resume: found_resume = True print "Resuming from %s" % resume else: words.put(word) return words
1 kaneg 2016-02-05 13:18:54 +08:00 很明显这个例子不完整。根据你的描述,我猜想它是要把当前的 word 放在一个存储中,如果中断后,从这个存储中将中断前的 word 加载到 resume 里,然后就可以用这一段逻辑来处理了 |
![]() | 2 lifemaxer OP @kaneg 我也是怀疑例子本身残缺,代码是从《 python 黑帽子》里摘录的,好几个案例都有这段队列函数,但 resume 在这里让人一头雾水,整个程序完全找不到控制 resume 的方法。 之所以发上来,是想着这会不会是 python 的特殊构造法,看来不是,是例子本身有问题。 |