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 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
Exemple #3
0
 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)
Exemple #4
0
 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))
Exemple #5
0
	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)
Exemple #6
0
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)
Exemple #7
0
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
 def inner(**kwargs):
     coro = func(**kwargs)
     Kernel().run(coro, shutdown=True)
Exemple #9
0
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
Exemple #10
0
def kernel(request):
    k = Kernel()
    request.addfinalizer(lambda: k.run(shutdown=True))
    return k
Exemple #11
0
# 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))
Exemple #12
0
def _get_kernel(selector=None):
    global _default_kernel
    selector = selector or eventloop.EventLoop()
    _default_kernel = Kernel(selector=selector)
    atexit.register(_shutdown_kernel)
Exemple #13
0
def get_new_kernel(**kwargs):
    selector = eventloop.EventLoop()
    kwargs["selector"] = selector
    return Kernel(**kwargs)
Exemple #14
0
 def setUp(self):
     self.k = Kernel()
Exemple #15
0
def kernel(request):
    k = Kernel()
    m = Monitor(k)
    request.addfinalizer(lambda: k.run(shutdown=True))
    request.addfinalizer(m.close)
    return k
Exemple #16
0
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)))
Exemple #17
0
    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)