推荐学习书目
› 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
saximi
V2EX  ›  Python

关于 Python 中协程的实现请教

  •  
  •   saximi · Nov 8, 2017 · 3372 views
    This topic created in 3244 days ago, the information mentioned may be changed or developed.
    ```
    看了廖雪峰关于协程的博文,但是关于其中的机制不理解。有几个问题请教。

    -------------问题 1-----------------
    假设我有以下两个方法:
    方法一:
    yield from 1 #语句 1
    yield from 2 #语句 2

    方法二:
    yield from a #语句 a
    yield from b #语句 b

    如果我在主程序中依序调用方法一和方法二,是否每次执行到 yield from 的时候线程就会去执行另外一个方法,导致语句的实际执行顺序如下?

    语句 1
    语句 a
    语句 2
    语句 b


    -------------问题 2-----------------
    协程中什么地方应该使用 yield from 语句?



    -------------问题 3-----------------
    恳请大家推荐一些讲解 PYTHON 下协程实现原理的好的资料,廖的文章觉得还是看不太懂。

    感谢大家!
    ```
    3 replies  •  2017-11-08 22:08:46 +08:00
    lxguidu
        1
    lxguidu  
       Nov 8, 2017
    流畅的 python
    akiakiseofficial
        2
    akiakiseofficial  
       Nov 8, 2017 via Android
    廖雪峰的 Python 教程中关于协程这一块就跟没讲一样,看《流畅的 Python 》吧
    hcnhcn012
        3
    hcnhcn012  
       Nov 8, 2017 via iPhone
    首先你得了解 yield 和生成器,曾经有一个非常经典的 producer 和 consumer 的例子,网上都有
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2537 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 01:08 · PVG 09:08 · LAX 18:08 · JFK 21:08
    ♥ Do have faith in what you're doing.