Coder Social home page Coder Social logo

Comments (3)

cyisfor avatar cyisfor commented on June 3, 2024
import bluelet

# Let's say there's a recursive generator...
# Either it generates a ValueEvent, or it delegates to the next level of recursion
# Shouldn't that ...sometimes... yield value events?

def justIteration():
    def listgetter(depth,i):
        if depth == 0:
            yield i
            return
        accum = 0
        for i in range(3):
            for thing in listgetter(depth-1,i):
                accum += thing
                yield accum
    for thing in listgetter(3,4):
        print(thing)
justIteration()

def justBlocking():
    def listgetter(depth,i):
        # some blocking socket operation here maybe.
        print("blocking yay")
        yield bluelet.sleep(1)
        yield bluelet.end(42)
    def main():
        value = yield listgetter(3,4)
        print(value)
    bluelet.run(main())

justBlocking()

def both():
    def listgetter(depth,i):
        if depth == 0:
            yield bluelet.end(i)
            return
        accum = 0
        for i in range(3):
            item = yield bluelet.call(listgetter(depth-1,i))
            # some blocking operation here.
            yield bluelet.sleep(0.1)
            accum += item
            print('Yielding a value event here!',accum)
            yield bluelet.ValueEvent(accum)
        yield bluelet.end(42)

    def main():
        for event in listgetter(3,4):
            if type(event)==bluelet.ValueEvent:
                # this is never called. Where are the value events?
                print('A value event was yielded.',event.value)
            else:
                # re-yield event?
                print('delegating')
                yield event

    bluelet.run(main())
both()

from bluelet.

cyisfor avatar cyisfor commented on June 3, 2024

OK I think this might be doing it. I don't even comprehend what I'm doing at this point. Have to look and see what the heck I did.

def both():
    def listgetter(depth,i):
        if depth == 0:
            yield bluelet.end(i)
            return
        accum = 0
        for i in range(3):
            for item in listgetter(depth-1,i):
                # some blocking operation here.
                if hasattr(item,'value'):
                    item = item.value
                    print(item)
                    yield bluelet.sleep(0.1)
                    accum += item
                else:
                    yield item
            print('Yielding a value event here!',depth,accum)
            yield bluelet.ValueEvent(accum)
        yield bluelet.end(accum)

    def main():
        for item in listgetter(3,4):
            if hasattr(item,'value'):
                print('A value was yielded.',item.value)
            else:
                yield item
    bluelet.run(main())
both()

from bluelet.

sampsyo avatar sampsyo commented on June 3, 2024

This is an interesting case—I haven't really built support for (nor thought of a good idea for how to support) using Bluelet within generators (coroutines) that you actually intend to use as generators. That is, Bluelet assumes that you're just using yield to generate events and that the Bluelet scheduler will have full control over them.

So, while I don't really know what the best way to combine iteration with Bluelet events is, your solution is actually a pretty neat hack. main passes most through events from listgetter to the scheduler, but intercepts certain events and consumes them before sending them. Nice idea—I don't really have a better one for now, so 👍 to this from me.

from bluelet.

Related Issues (6)

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.