Coder Social home page Coder Social logo

bluelet's People

Contributors

arcresu avatar sampsyo avatar samrushing avatar wezzman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bluelet's Issues

Event Scheduling

More of a question, than an issue. Is it possible to do something like Twisted's callLater or JavaScript's setTimeout with bluelet?

Terminating bluelet running server

Is there any way to exit a running bluelet.run(bluelet.server(host, port, handler_coro))? I have a bunch in separate threads and I need a way to get them to exit with the main thread

Trying to iterate recursively, and use blocking operations arg

I'm trying to do a thing that iterates through a tree, possibly a big tree, possibly one I wouldn't want to load all into memory at once, and it might be partially on the cloud so I wanted to pause in places to wait for items to come in.

So I can figure out how to iterate recursively using yield, and how to use bluelet to block on events using yield, but not to do both!

I tried to illustrate what I'm talking about with a program, attached...

Joining on already exited coroutine blocks indefinetely

I am trying to use a looping and sleeping coroutine as a synchronization primitive and it works fairly well for synchronous and timed cases. But when I try to use it in an Asynchronous case with a long wait before trying to get the result the looping coroutine may have exited. If it did the future like coroutine I'm using blocks while trying to join on the looping coroutine indefinitely.

import bluelet
import random

class TestProxy:
    _suspeneded = {}
    _messages = {}
    _continue_exec = True

    def init(self):
        print "Initializing"
        while TestProxy._continue_exec:
            print "Checking for messages"
            for k in TestProxy._messages.keys():
                if random.randint(0,100) < 50:
                    if k in TestProxy._suspeneded:
                        TestProxy._messages[k] = 0
                        print "Message sent {}".format(k)
                        yield bluelet.kill(TestProxy._suspeneded[k])
                        del TestProxy._suspeneded[k]
                        print "Killed {}".format(k)
            yield bluelet.sleep(1)
        print "Shutting down"

    def call_sync(self, num):
        # Setup
        TestProxy._messages[self] = None
        print "{} was passed".format(num)

        def inner_call_sync(self):
            while TestProxy._messages[self] is None:
                print "Sleeping {}".format(self)
                yield bluelet.sleep(600)

        # Create wait coroutine
        coro = inner_call_sync(self)
        # Store reference to wait coroutine
        TestProxy._suspeneded[self] = coro
        # Start and block on wait coroutine
        yield coro

        # Get message a cleanup
        print "Message Received {}".format(TestProxy._messages[self])
        message = TestProxy._messages.pop(self)
        yield bluelet.end(message)

    def call_timeout(self, num, timeout):
        # Setup
        TestProxy._messages[self] = None
        print "{} was passed".format(num)

        def inner_call_timeout(self, timeout):
            print "Sleeping {} for {}".format(self, timeout)
            yield bluelet.sleep(timeout)

        # Create wait coroutine
        coro = inner_call_timeout(self, timeout)
        # Store reference to wait coroutine
        TestProxy._suspeneded[self] = coro
        # Start and block on wait coroutine
        yield coro

        # Get message a cleanup
        print "Message Received {}".format(TestProxy._messages[self])
        message = TestProxy._messages.pop(self)
        yield bluelet.end(message)

    def call_async(self, num):
        # Setup
        TestProxy._messages[self] = None
        print "{} was passed".format(num)

        def inner_call_async(self):
            while TestProxy._messages[self] is None:
                print "Sleeping {}".format(self)
                yield bluelet.sleep(600)

        # Create wait coroutine
        coro = inner_call_async(self)
        # Store reference to wait coroutine
        TestProxy._suspeneded[self] = coro
        # Start wait coroutine
        yield bluelet.spawn(coro)

        def inner_finish(self, coro):
            print "Waiting on future"
            yield bluelet.join(coro)
            # Get message a cleanup
            print "Message Received {}".format(TestProxy._messages[self])
            message = TestProxy._messages.pop(self)
            yield bluelet.end(message)

        # Returning future
        yield bluelet.end(inner_finish(self, coro))


    def shutdown(self):
        TestProxy._continue_exec = False

def test(num):
    print "Number is {}".format(num)

    tp = TestProxy()

    ret = yield tp.call_sync(num)

    print "Returned {}".format(ret)

def test2(num):
    print "Number is {}".format(num)

    tp = TestProxy()

    ret = yield tp.call_timeout(num, 2)

    if ret is None:
        print "Call timed out"
    else:
        print "Returned {}".format(ret)

def test3(num):
    print "Number is {}".format(num)

    tp = TestProxy()

    fut = yield tp.call_async(num)

    yield bluelet.sleep(60)

    print "Async called"

    ret = yield fut

    print "Returned {}".format(ret)

def main():
    yield bluelet.spawn(TestProxy().init())
    #yield bluelet.spawn(test(42))
    #yield bluelet.spawn(test(43))
    #yield bluelet.spawn(test2(44))
    #yield bluelet.spawn(test2(45))
    yield bluelet.spawn(test3(46))
    yield bluelet.spawn(test3(47))
    yield bluelet.sleep(300)
    TestProxy().shutdown()

bluelet.run(main())

I think I know how to fix this though. If you add a check to see if the coroutine you want to join has been scheduled before suspending, finished coroutines could join immediately. This does not take into account coroutines that have not been spawned yet but that can most likely be solved by using a weakref.WeakKeyDictionary containing a record of all previously scheduled coroutines that still have references elsewhere.

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.