def main(): value = yield ordinary() print(value) value = yield square(5) print(value) try: yield fail() except Exception as e: print("Caught exception:", type(e), str(e)) try: yield invalid_yield() except InvalidYieldException as e: print("Caught exception:", type(e), str(e)) else: assert False def func_fail(): raise Exception("func boo") @_o def example(): monocle.launch(fail) monocle.launch(func_fail) yield main() run(example)
from __future__ import print_function import sys import monocle monocle.init(sys.argv[1]) from monocle.script_util import run from monocle import _o from monocle.stack.network.http import HttpClient @_o def example(): client = HttpClient() yield client.connect("www.google.com", 443, "https") print("connected") resp = yield client.request('/') print("response code -> %s" % resp.code) print("page length -> %s" % len(repr(resp.body))) client.close() run(example)
print('Running in two') await asyncio.sleep(0) print('Done with two') return await square(2) @_o def eight(): x = yield four() yield Return(x) async def fail(): raise Exception("boo") print(await square(2)) @_o def invalid_yield(): yield "this should fail" @_o def main(): value = yield eight() print(value) yield fail() run(main)
@_o def handle_echo(conn): while True: try: message = yield conn.read_until('\r\n') except ConnectionLost: break yield conn.write("you said: %s\r\n" % message.strip()) @_o def do_echos(): try: client = Client() yield client.connect('localhost', 8000) t = time.time() for x in xrange(10000): msg = "hello, world #%s!" % x yield client.write(msg + '\r\n') echo_result = yield client.read_until("\r\n") assert echo_result.strip() == "you said: %s" % msg print('10000 loops in %.2fs' % (time.time() - t)) finally: client.close() add_service(Service(handle_echo, port=8000)) run(do_echos)
def fifth(): die() def fourth(): return fifth() @_o def third(): yield fourth() def second(): return third() @_o def first(): yield second() @_o def first_evlp(): yield sleep(1) yield req() yield launch(second) # won't crash run(first_evlp)