V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lifemaxer
V2EX  ›  Python

请教本例子中用 resume 恢复会话的原理

  •  
  •   lifemaxer · 2016-02-04 15:34:48 +08:00 · 2440 次点击
    这是一个创建于 3005 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在书中看到这个例子,引入 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
    
    2 条回复    2016-02-05 21:50:20 +08:00
    kaneg
        1
    kaneg  
       2016-02-05 13:18:54 +08:00
    很明显这个例子不完整。根据你的描述,我猜想它是要把当前的 word 放在一个存储中,如果中断后,从这个存储中将中断前的 word 加载到 resume 里,然后就可以用这一段逻辑来处理了
    lifemaxer
        2
    lifemaxer  
    OP
       2016-02-05 21:50:20 +08:00
    @kaneg 我也是怀疑例子本身残缺,代码是从《 python 黑帽子》里摘录的,好几个案例都有这段队列函数,但 resume 在这里让人一头雾水,整个程序完全找不到控制 resume 的方法。
    之所以发上来,是想着这会不会是 python 的特殊构造法,看来不是,是例子本身有问题。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3014 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:44 · PVG 21:44 · LAX 06:44 · JFK 09:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.