V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
petelin
V2EX  ›  Python

只用 Python 的 yield 关键字, 实现一个协程, 或者说用户态函数(生成器)调度

  •  
  •   petelin · Oct 28, 2017 · 3841 views
    This topic created in 3112 days ago, the information mentioned may be changed or developed.
    关于协程 sleep 实现想了好久, idea 来源于 Python 社区的一个大牛, 当时看过一遍, 现在想的比较清楚就写出来了--


    8 replies    2017-10-30 11:53:59 +08:00
    petelin
        1
    petelin  
    OP
       Oct 28, 2017
    最后有一个 tcp server 的实现, 类似于 gevent 的并发,在 handler 里写 sleep 不会被阻塞
    mdzz
        2
    mdzz  
       Oct 28, 2017
    有点意思
    czheo
        3
    czheo  
       Oct 28, 2017
    lz 应该是看了这个吧,代码思路最后一部分一样。
    David Beazley - Python Concurrency From the Ground Up: LIVE! - PyCon 2015
    wcsjtu
        4
    wcsjtu  
       Oct 28, 2017
    核心就是用 poll 做异步定时器~ 和 yield 没多大关系吧。lz 可以看看 tornado 的 sleep 实现
    czheo
        5
    czheo  
       Oct 28, 2017
    @wcsjtu 这种写法的初衷是解决多任务(concurrency),poll/select 控制异步调度,yield 实现多任务。
    petelin
        6
    petelin  
    OP
       Oct 29, 2017 via Android
    @wcsjtu 好的,我去看看,也不是异步定时器,而是异步通知,yield 正如 czheo 说的,是多任务,没有多任务,写出来的也只是 event loop 而已。
    petelin
        7
    petelin  
    OP
       Oct 29, 2017 via Android
    @czheo 应该是有一个 PPT,从 yield 开始讲的。基本都是从他那学的。关于 sleep 部分实现是我想的。
    lolizeppelin
        8
    lolizeppelin  
       Oct 30, 2017
    粗看了下 eventlet 的做法差不多...用 yield 写起来太别扭了才有 greenlet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3749 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 10:38 · PVG 18:38 · LAX 03:38 · JFK 06:38
    ♥ Do have faith in what you're doing.