def kernel(request): """Provide a Curio Kernel object for running co-routines.""" k = Kernel(debug=[longblock, logcrash]) m = monitor.Monitor(k) request.addfinalizer(lambda: k.run(shutdown=True)) request.addfinalizer(m.close) return k
def up(self): kernel = Kernel() kernel.run(h2_server(address=(self.address, self.port), certfile="{}.crt.pem".format("localhost"), keyfile="{}.key".format("localhost"), app=self), shutdown=True)
def start(self): kernel = Kernel() kernel.run(h2_server(address=(self.address, self.port), certfile=self.certfile, keyfile=self.keyfile, app=self), shutdown=True)
def up(self): kernel = Kernel() print("Try GETting:") print(" (Accept all the warnings)") kernel.run( h2_server(address=("localhost", self.port), root=self.root, certfile="{}.crt.pem".format("localhost"), keyfile="{}.key".format("localhost"), app=self))
def up(self): """ Start the server. This is the last function users should call. Users only call this function after set up all routing handlers. """ kernel = Kernel() kernel.run(h2_server(address=(self.address, self.port), certfile="{}.crt.pem".format("localhost"), keyfile="{}.key".format("localhost"), app=self), shutdown=True)
def test_activation_crash(): async def main(): await sleep(0.01) raise ValueError("Dead") a = _TestActivation() kern = Kernel(activations=[a]) try: kern.run(main) assert False except ValueError as e: assert a.events == ['activate', 'created', 'running', 'suspended', 'running', 'suspended', 'terminated'] kern.run(shutdown=True)
def test_run_curio(): counter = ContextVar("counter", default=5) async def reset_counter(): if counter.get() == 5: counter.set(0) return 0 return -1 with Kernel() as k: assert run(k, reset_counter) == 0 assert run(k, reset_counter) == 0 assert counter.get() == 5 assert counter.get() == 5
# echoserv.py # # Echo server using streams from curio import Kernel, new_task, run_server async def echo_client(client, addr): print('Connection from', addr) reader, writer = client.make_streams() async with reader, writer: async for line in reader: await writer.write(line) print('Connection closed') if __name__ == '__main__': kernel = Kernel() kernel.run(run_server('', 25000, echo_client))
from curio import Kernel, new_task, run_cpu_bound, run_server def fib(n): if n <= 2: return 1 else: return fib(n-1) + fib(n-2) async def fib_handler(client, addr): print('Connection from', addr) rfile, wfile = client.make_streams() async with rfile, wfile: async for line in rfile: try: n = int(line) result = await run_cpu_bound(fib, n) resp = str(result) + '\n' await wfile.write(resp.encode('ascii')) except ValueError: await wfile.write(b'Bad input\n') print('Connection closed') if __name__ == '__main__': kernel = Kernel() kernel.run(run_server('', 25000, fib_handler))
# A simple echo server from curio import Kernel, new_task, run_server async def echo_handler(client, addr): print('Connection from', addr) while True: data = await client.recv(10000) if not data: break await client.sendall(data) print('Connection closed') if __name__ == '__main__': kernel = Kernel() kernel.run(run_server('', 25000, echo_handler))
class TestRouter(unittest.TestCase): def setUp(self): self.k = Kernel() def tearDown(self): async def f(): return self.k.run(f, shutdown=True) def test_raise_error_on_non_existing_route(self): """ If a route doesn't exist and no default method is given, should raise error """ router = Router(None) stream = Stream(1, {':path': 'x', ':method': 'GET'}) async def f(): with self.assertRaises(RouteNotRegisteredException): await router.handle_route(None, stream) self.k.run(f()) def test_get_existing_route(self): router = Router(None) async def f(req, res): assert req.stream.headers[':path'] == 'x' assert res.http is None router.register('GET', route='x', handler=f) stream = Stream(1, {':path': 'x', ':method': 'GET'}) coroutine = router.handle_route(None, stream) self.k.run(coroutine) def test_post_existing_route(self): router = Router(None) async def f(req, res): assert res.http is None assert req.stream.headers[':path'] == 'x' router.register('POST', route='x', handler=f) stream = Stream(1, {':path': 'x', ':method': 'POST'}) coroutine = router.handle_route(None, stream) self.k.run(coroutine) def test_match(self): # match true matched, parameters = Router._match('user/{userId}/name/{name}', 'user/123/name/John') self.assertTrue(matched) self.assertEqual(parameters['userId'], '123') self.assertEqual(parameters['name'], 'John') # match false matched, parameters = Router._match('user/{userId}/name/{name}', 'user/123/nam/John') self.assertFalse(matched) def test_parameterized_route(self): router = Router(None) async def f(req, res): self.assertIsNone(res.http) self.assertEqual(req.para['userId'], '123') self.assertEqual(req.para['name'], 'John') router.register('GET', route='user/{userId}/name/{name}', handler=f) stream = Stream(1, {':path': 'user/123/name/John', ':method': 'GET'}) c = router.handle_route(None, stream) self.k.run(c)
import unittest from curio import Kernel from hyper2web.http import Stream, Request, Response from hyper2web.router import Router k = Kernel() class TestRouter(unittest.TestCase): def test_raise_error_on_non_existing_route(self): """If a route doesn't exist, should raise error""" router = Router(None) stream = Stream(1, {':path': 'x', ':method': 'GET'}) # should raise a more specific error in the future with self.assertRaises(Exception): coroutine = router.handle_route(None, stream) k.run(coroutine) def test_get_existing_route(self): router = Router(None) async def f(req, res): assert req.stream.headers[':path'] == 'x' assert res.http is None router.register('GET', route='x', handler=f) stream = Stream(1, {':path': 'x', ':method': 'GET'})
from curio import Kernel from async_btree import FAILURE, parallele async def a_func(): print("\ta_func " + str(threading.get_ident())) await sleep(0.5) return 'a' async def b_func(): print("\tb_func " + str(threading.get_ident())) await sleep(1) return 'b' async def failure_func(): print("\tfailure_func " + str(threading.get_ident())) await sleep(0.75) return FAILURE k = Kernel() tree = parallele(children=[a_func, b_func]) result = k.run(tree) print(f"result={result}") k.run(shutdown=True)
if stream_id and stream_id in self.flow_control_events: evt = self.flow_control_events.pop(stream_id) await evt.set() elif not stream_id: # Need to keep a real list here to use only the events present at # this time. blocked_streams = list(self.flow_control_events.keys()) for stream_id in blocked_streams: event = self.flow_control_events.pop(stream_id) await event.set() return if __name__ == '__main__': host = sys.argv[2] if len(sys.argv) > 2 else "localhost" kernel = Kernel(with_monitor=True) kernel.add_task( h2_server((host, 5000), sys.argv[1], "{}.crt.pem".format(host), "{}.key".format(host))) print("Try GETting:") print( " On OSX after 'brew install curl --with-c-ares --with-libidn --with-nghttp2 --with-openssl':" ) print( "/usr/local/Cellar/curl/7.47.1/bin/curl --tlsv1.2 --http2 -k https://localhost:5000/bundle.js" ) print("Or open a browser to: https://localhost:5000/") print(" (Accept all the warnings)") kernel.run()
def _get_kernel(selector=None): global _default_kernel selector = selector or eventloop.EventLoop() _default_kernel = Kernel(selector=selector) atexit.register(_shutdown_kernel)
def get_new_kernel(**kwargs): selector = eventloop.EventLoop() kwargs["selector"] = selector return Kernel(**kwargs)
# Example: A simple Unix echo server from curio import Kernel, run_unix_server async def echo_handler(client, address): print('Connection from', address) while True: data = await client.recv(10000) if not data: break await client.sendall(data) print('Connection closed') if __name__ == '__main__': kernel = Kernel(with_monitor=True) import os try: os.remove('/tmp/curiounixecho') except: pass kernel.run(run_unix_server('/tmp/curiounixecho', echo_handler))
def kernel(request): k = Kernel() request.addfinalizer(lambda: k.run(shutdown=True)) return k
async def wait_for_flow_control(self, stream_id): """ Blocks until the flow control window for a given stream is opened. """ evt = Event() self.flow_control_events[stream_id] = evt await evt.wait() async def window_updated(self, event): """ Unblock streams waiting on flow control, if needed. """ stream_id = event.stream_id if stream_id and stream_id in self.flow_control_events: evt = self.flow_control_events.pop(stream_id) await evt.set() elif not stream_id: for evt in self.flow_control_events.values(): await evt.set() self.flow_control_events = {} return if __name__ == '__main__': kernel = Kernel() kernel.add_task(h2_server(('', 8080), sys.argv[1])) kernel.run()
def kernel(request): k = Kernel() m = Monitor(k) request.addfinalizer(lambda: k.run(shutdown=True)) request.addfinalizer(m.close) return k
# Example: A simple echo server written using streams from curio import Kernel, new_task from curio.socket import * async def echo_server(address): sock = socket(AF_INET, SOCK_STREAM) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind(address) sock.listen(5) print('Server listening at', address) async with sock: while True: client, addr = await sock.accept() print('Connection from', addr) await new_task(echo_client(client)) async def echo_client(client): reader, writer = client.make_streams() async with reader, writer: async for line in reader: await writer.write(line) await client.close() print('Connection closed') if __name__ == '__main__': kernel = Kernel() kernel.run(echo_server(('',25000)))
def kernel(request): k = Kernel(debug=[longblock, logcrash, schedtrace, traptrace]) m = Monitor(k) request.addfinalizer(lambda: k.run(shutdown=True)) request.addfinalizer(m.close) return k
def setUp(self): self.k = Kernel()
def inner(**kwargs): coro = func(**kwargs) Kernel().run(coro, shutdown=True)
async def tick(): value = counter.get() counter.set(value - 1) if value <= 0: return FAILURE if value == 3: raise RuntimeError('3') return SUCCESS assert kernel.run(repeat_until( condition=tick, child=a_func)) == 'a', 'return last sucess result' assert counter.get() == 2 from curio import Kernel k = Kernel() tree1 = sequence(children=[a_func, failure_func, success_func]) r = k.run(tree1) print(f"result={r}") tree2 = selector(children=[exception_func, failure_func, a_func]) r = k.run(tree2) print(f"result={r}") assert k.run(decision(condition=success_func, success_tree=a_func)) == 'a' print("===") test_repeat_until_falsy_condition(k) k.run(shutdown=True)
def kernel(request): k = Kernel(debug=[longblock, logcrash]) m = Monitor(k) request.addfinalizer(lambda: k.run(shutdown=True)) request.addfinalizer(m.close) return k
""" Unblock streams waiting on flow control, if needed. """ stream_id = event.stream_id if stream_id and stream_id in self.flow_control_events: evt = self.flow_control_events.pop(stream_id) await evt.set() elif not stream_id: # Need to keep a real list here to use only the events present at # this time. blocked_streams = list(self.flow_control_events.keys()) for stream_id in blocked_streams: event = self.flow_control_events.pop(stream_id) await event.set() return if __name__ == '__main__': host = sys.argv[2] if len(sys.argv) > 2 else "localhost" kernel = Kernel(with_monitor=True) print("Try GETting:") print(" On OSX after 'brew install curl --with-c-ares --with-libidn --with-nghttp2 --with-openssl':") print("/usr/local/opt/curl/bin/curl --tlsv1.2 --http2 -k https://localhost:5000/bundle.js") print("Or open a browser to: https://localhost:5000/") print(" (Accept all the warnings)") kernel.run(h2_server((host, 5000), sys.argv[1], "{}.crt.pem".format(host), "{}.key".format(host)))
if stream_id and stream_id in self.flow_control_events: evt = self.flow_control_events.pop(stream_id) await evt.set() elif not stream_id: # Need to keep a real list here to use only the events present at # this time. blocked_streams = list(self.flow_control_events.keys()) for stream_id in blocked_streams: event = self.flow_control_events.pop(stream_id) await event.set() return if __name__ == '__main__': host = sys.argv[2] if len(sys.argv) > 2 else "localhost" print(sys.argv) kernel = Kernel() print("Try GETting:") print( " On OSX after 'brew install curl --with-c-ares --with-libidn --with-nghttp2 --with-openssl':" ) print( "/usr/local/opt/curl/bin/curl --tlsv1.2 --http2 -k https://localhost:5000/bundle.js" ) print("Or open a browser to: https://localhost:5000/") print(" (Accept all the warnings)") kernel.run( h2_server((host, 5000), sys.argv[1], "{}.crt.pem".format(host), "{}.key".format(host)))
from curio import Kernel, new_task from curio.socket import * async def echo_server(address): sock = socket(AF_INET, SOCK_STREAM) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind(address) sock.listen(5) print('Server listening at', address) async with sock: while True: client, addr = await sock.accept() print('Connection from', addr) await new_task(echo_client(client)) async def echo_client(client): async with client: while True: data = await client.recv(10000) if not data: break await client.sendall(data) print('Connection closed') if __name__ == '__main__': kernel = Kernel() kernel.run(echo_server(('', 25000)))
# Echo server with cancellation and signal handling import signal from curio import Kernel, new_task, SignalSet, CancelledError, run_server async def echo_client(client, addr): print('Connection from', addr) try: while True: data = await client.recv(1000) if not data: break await client.sendall(data) print('Connection closed') except CancelledError: await client.sendall(b'Server going down\n') async def main(host, port): while True: async with SignalSet(signal.SIGHUP) as sigset: print('Starting the server') serv_task = await new_task(run_server(host, port, echo_client)) await sigset.wait() print('Server shutting down') await serv_task.cancel_children() await serv_task.cancel() if __name__ == '__main__': kernel = Kernel(with_monitor=True) kernel.run(main('', 25000))