Giter Club home page Giter Club logo

Comments (2)

Drincann avatar Drincann commented on July 19, 2024

@wwhai

你需要自己实现这个 sleep,自行维护这些计时任务。换句话说,sleep 是线程级阻塞,你需要实现一个协程级阻塞。

刚才简单读了下代码,大佬的 coroutine 在我的知识框架内实际上是生成器(generator)的概念,而生成器确实是 stackless 协程的一个重要组成部分。

拿 js 举例,js 的运行时(例如 nodejs)实现的协程调度器的主要部件是一个事件循环,里面通过 IO 复用器来避免线程级 IO 阻塞,从而实现单线程并发。你可以参考这个实现,这是我用 python 的生成器和 py 封装的上层 selector 支持实现的无栈协程的事件循环部分。

注意到在循环内首先调度了计时器:

# check timer heap
while (timer := timerHeap.peekTimer()) is not None and timer.isTimeout():
    self.__execTask(timerHeap.popTimer().getCallback())

这就是我刚才提到的协程级阻塞,当协程(生成器)从 sleep 处 yield 后会交还控制权给调度器,调度器负责将一个计时器实体维护起来,在后面的事件循环中尝试重新调度,这时候线程并没有阻塞,所以我们的调度器可以立刻去调度其它可以恢复的协程,但刚刚挂起的协程却"认为"自己阻塞了,这也就实现了你所说的"让其不阻塞"。

对计时器来说,工业界的常规做法是用一个最小堆来维护,最近时间最小,于是可以 O(1) query,O(logn) modify,这里是类似 sleep 的实现:

@LoopManager.asyncapi
def setTimeout(timeoutms, callback, asyncDone):
    _timerHeap.pushTimer(Timer(timeoutms / 1000, callback, asyncDone))

他做的事情非常简单,就是把一个封装好的 timer 推到一个顶堆里,事件循环会在合适的时机 check 这个顶堆。

from coroutine.

wwhai avatar wwhai commented on July 19, 2024

@wwhai

你需要自己实现这个 sleep,自行维护这些计时任务。换句话说,sleep 是线程级阻塞,你需要实现一个协程级阻塞。

刚才简单读了下代码,大佬的 coroutine 在我的知识框架内实际上是生成器(generator)的概念,而生成器确实是 stackless 协程的一个重要组成部分。

拿 js 举例,js 的运行时(例如 nodejs)实现的协程调度器的主要部件是一个事件循环,里面通过 IO 复用器来避免线程级 IO 阻塞,从而实现单线程并发。你可以参考这个实现,这是我用 python 的生成器和 py 封装的上层 selector 支持实现的无栈协程的事件循环部分。

注意到在循环内首先调度了计时器:

# check timer heap
while (timer := timerHeap.peekTimer()) is not None and timer.isTimeout():
    self.__execTask(timerHeap.popTimer().getCallback())

这就是我刚才提到的协程级阻塞,当协程(生成器)从 sleep 处 yield 后会交还控制权给调度器,调度器负责将一个计时器实体维护起来,在后面的事件循环中尝试重新调度,这时候线程并没有阻塞,所以我们的调度器可以立刻去调度其它可以恢复的协程,但刚刚挂起的协程却"认为"自己阻塞了,这也就实现了你所说的"让其不阻塞"。

对计时器来说,工业界的常规做法是用一个最小堆来维护,最近时间最小,于是可以 O(1) query,O(logn) modify,这里是类似 sleep 的实现:

@LoopManager.asyncapi
def setTimeout(timeoutms, callback, asyncDone):
    _timerHeap.pushTimer(Timer(timeoutms / 1000, callback, asyncDone))

他做的事情非常简单,就是把一个封装好的 timer 推到一个顶堆里,事件循环会在合适的时机 check 这个顶堆。

明白了

from coroutine.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.