sampsyo / bluelet Goto Github PK
View Code? Open in Web Editor NEWsimple asynchronous socket I/O with Python coroutines
simple asynchronous socket I/O with Python coroutines
More of a question, than an issue. Is it possible to do something like Twisted's callLater
or JavaScript's setTimeout
with bluelet?
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
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...
Trying to exit from loop in Windows CLI and it does not exit.
C:\Python35-64\python.exe blue_test.py
"Ctrl-C" or "Ctrl-Z" does nothing
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.
I am curious whether bluelets has support or plans to support an interface similar to gevent.threadpool. Is this possible?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.