Example #1
0
def test_loop_keep_alive_normal_death():
    v = [0]

    def l():
        v[0] += 1

    def p():
        sleep(0.9)
        WVPASS(v[0] > 1)
        a.halt()

    a = Application()
    a.add_loop(Loop(l), keep_alive=True)
    a.add_loop(Loop(p))
    a.run()
Example #2
0
File: cli.py Project: dowski/aspen
def main(argv=None):
    try:
        configuration = Configuration(argv)
        configuration.app = app = Application()
        website = Website(configuration)
        configuration.website = website  # to support re-handling, especially
        website = configuration.hooks.run('startup', website)

        # change current working directory
        os.chdir(configuration.root)

        if configuration.conf.aspen.no('changes_kill'):
            # restart for template files too;
            # TODO can't we just invalidate the simplate cache for these?
            dot_aspen = join(configuration.root, '.aspen')
            for root, dirs, files in os.walk(dot_aspen):
                for filename in files:
                    restarter.add(join(root, filename))

            app.add_loop(Loop(restarter.loop))

        port = configuration.address[1]
        app.add_service(Service(http.HttpServer(website), port))

        log.warn("Greetings, program! Welcome to port %d." % port)
        app.run()

    except KeyboardInterrupt, SystemExit:
        configuration.hooks.run('shutdown', website)
Example #3
0
def test_loop_keep_alive_normal_death():
    v = [0]

    def l():
        v[0] += 1

    def p():
        sleep(0.7)

    runtime.current_app.add_loop(Loop(l), keep_alive=True)
    lp = Loop(p)
    runtime.current_app.add_loop(lp)
    sleep()
    while lp.running:
        sleep()
    assert (v[0] > 1)
Example #4
0
def test_loop_keep_alive_exception():
    v = [0]

    def l():
        v[0] += 1
        a = b  # exception!

    def p():
        sleep(0.9)
        WVPASS(v[0] > 1)
        a.halt()

    a = Application()
    a.add_loop(Loop(l), keep_alive=True)
    a.add_loop(Loop(p))
    a.run()
Example #5
0
def test_loop_keep_alive_exception():
    v = [0]

    def l():
        v[0] += 1
        a = b  # exception!

    def p():
        sleep(0.7)

    runtime.current_app.add_loop(Loop(l), keep_alive=True)
    lp = Loop(p)
    runtime.current_app.add_loop(lp)
    sleep()
    while lp.running:
        sleep()
    assert (v[0] > 1)
Example #6
0
def main():
    app = Application()
    app.add_loop(Loop(santa))

    elf_do = "meets in study"
    for i in xrange(10):
        app.add_loop(Loop(actor("Elf %d" % i, 'elf', elf_group, elf_do, 3, 3)))

    deer_do = "delivers toys"
    for name in [
            'Dasher', 'Dancer', 'Prancer', 
            'Vixen', 'Comet', 'Cupid', 
            'Donner', 'Blitzen', 'Rudolph',
            ]:
        app.add_loop(Loop(actor(name, 'deer', deer_group, deer_do, 9, 9)))

    app.run()
Example #7
0
def req_loop():
    global links
    client, heads = get_client()
    log.info(path)
    code, heads, body = client.request('GET', path, heads)
    write_file(path, body)
    links = get_links(body)
    for x in xrange(CONCURRENCY):
        a.add_loop(Loop(follow_loop))
Example #8
0
def test_child_keep_alive_retains_parent():
    children = [None]
    parent = Loop(protective_parent, flapping_child, children)
    runtime.current_app.add_loop(parent)
    sleep()
    while parent.running:
        # Deaths of the child are interspersed here.
        assert parent.children
        assert children[0].parent is parent
        sleep()
    # The child should have died a bunch of times during the parent's life.
    assert children[0].deaths > 1, children[0].deaths
Example #9
0
    def test_basic_sleep(self):
        app, touch, acc = self.prepare_test()

        def loop():
            t = time.time()
            sleep(1)
            acc.duration = time.time() - t
            touch()

        app.add_loop(Loop(loop))
        self.run_test()
        assert 1 < (acc.duration + 0.1) < 1.5
Example #10
0
    def test_basic_loops(self):
        NUM = 500
        TOUCHES = 5
        app, touch, acc = self.prepare_test()
        def loop():
            for x in xrange(TOUCHES):
                sleep(0.1)
                touch()

        for x in xrange(NUM):
            app.add_loop(Loop(loop))

        self.run_test(NUM * TOUCHES)
Example #11
0
File: app.py Project: wmoss/diesel
def quickstart(*args):
    app = Application()
    args = list(args)
    for a in args:
        if isinstance(a, list):
            args.extend(a)
        elif isinstance(a, Service):
            app.add_service(a)
        elif isinstance(a, Loop):
            app.add_loop(a)
        elif callable(a):
            app.add_loop(Loop(a))
    app.run()
Example #12
0
def test_fork_child_exception():
    got_exception = [0]

    def parent():
        fork_child(dependent_child, got_exception)
        sleep(0.1)
        a = b  # undef

    l = Loop(parent)
    runtime.current_app.add_loop(l)
    sleep()  # give the Loop a chance to start
    while l.running:
        sleep()
    assert got_exception[0], "child didn't die when parent died!"
Example #13
0
def test_basic_fork():
    v = [0]
    done = Event()

    def parent():
        fork(tottering_child, v)
        sleep(0.1)
        done.set()

    runtime.current_app.add_loop(Loop(parent))
    ev, _ = first(sleep=1, waits=[done])
    if ev == 'sleep':
        assert 0, "timed out"
    assert (v[0] == 1)
Example #14
0
def test_fork_many():
    v = [0]
    COUNT = 10000

    def parent():
        for x in xrange(COUNT):
            fork(tottering_child, v)

    runtime.current_app.add_loop(Loop(parent))

    for i in xrange(16):
        if v[0] == COUNT:
            break
        sleep(0.5)  # cumulative is long enough in core 2-era
    else:
        assert 0, "didn't reach expected COUNT soon enough"
Example #15
0
    def test_multiple_sleep(self):
        app, touch, acc = self.prepare_test()

        def loop():
            t = time.time()
            sleep(1)
            acc.durations.append(time.time() - t)
            touch()

        acc.durations = []
        NUM = 5
        for x in xrange(NUM):
            app.add_loop(Loop(loop))
        self.run_test(NUM)

        for d in acc.durations:
            assert 1 < (d + 0.1) < 1.5
Example #16
0
def quickstart(*args, **kw):
    if '__app' in kw:
        app = kw.pop('__app')
    else:
        app = Application(**kw)
    args = list(args)
    for a in args:
        if isinstance(a, Thunk):
            a = a.eval()
        if isinstance(a, (list, tuple)):
            args.extend(a)
        elif isinstance(a, Service):
            app.add_service(a)
        elif isinstance(a, Loop):
            app.add_loop(a)
        elif callable(a):
            app.add_loop(Loop(a))
    app.run()
Example #17
0
def test_child_keep_alive_dies_with_parent():
    # Starts a child that attempts to run for a long time, with a parent
    # that has a short lifetime. The rule is that children are always
    # subordinate to their parents.
    children = [None]
    parent = Loop(protective_parent, strong_child, children)
    runtime.current_app.add_loop(parent)
    sleep()
    while parent.running:
        sleep()

    # Wait here because a child could respawn after 0.5 seconds if the
    # child-always-dies-with-parent rule is being violated.
    sleep(1)

    # Once the parent is dead, the child (even thought it is keep-alive)
    # should be dead as well.
    assert not children[0].running
    assert not parent.children
Example #18
0
class UDPService(Service):
    '''A UDP service listening on a certain port, with a protocol
    implemented by a passed connection handler.
    '''
    def __init__(self, connection_handler, port, iface=''):
        Service.__init__(self, connection_handler, port, iface)
        self.remote_addr = (None, None)

    def bind_and_listen(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        # unsure if the following two lines are necessary for UDP
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.setblocking(0)

        try:
            sock.bind((self.iface, self.port))
        except socket.error, e:
            self.handle_cannot_bind(str(e))

        self.sock = sock
        c = UDPSocket(self, sock)
        l = Loop(self.connection_handler)
        l.connection_stack.append(c)
        runtime.current_app.add_loop(l)
Example #19
0
        assert r.hexists("h1", "bahbah") == True
        assert r.hexists("h1", "nope") == False

        r.hdel("h1", "bahbah")
        assert r.hexists("h1", "bahbah") == False
        assert r.hlen("h1") == 3

        assert r.hkeys("h1") == set(['foo', 'baz', 'count'])
        assert set(r.hvals("h1")) == set(['bar', 'bosh', '7'])
        assert r.hgetall("h1") == {'foo': 'bar', 'baz': 'bosh', 'count': '7'}
        print 'all tests pass.'

        a.halt()

    a.add_loop(Loop(do_set))
    a.run()


#########################################
## Hub, an abstraction of sub behavior, etc
class RedisSubHub(object):
    def __init__(self, host='127.0.0.1', port=REDIS_PORT):
        self.host = host
        self.port = port
        self.sub_wake_signal = uuid.uuid4().hex
        self.sub_adds = []
        self.sub_rms = []
        self.subs = {}

    def make_client(self):
Example #20
0
 def make_connection():
     c = Connection(sock, addr)
     l = Loop(self.connection_handler, addr)
     l.connection_stack.append(c)
     runtime.current_app.add_loop(l)
Example #21
0
        assert count == 10000, count
        assert passes == 2, passes
        print "query7 (get_more) passed"
        print "inserting"
        d.diesel.test.insert([{'letter':'m'}, {'letter':'b'}, {'letter':'k'}])
        with d.diesel.test.find({'letter': {'$exists':True}}) as cursor:
            cursor.sort('letter', Ops.DESCENDING)
            while not cursor.finished:
                res = cursor.more()
                assert len(res) == 3, res
                assert [r['letter'] for r in res] == ['m', 'k', 'b'], res
                print "query8 (sorting) passed"
        with d.diesel.test.find({'type':'test'}) as cursor:
            n = cursor.count()
            assert n == 10000, n
            print "query9 (count) passed"
        n = 0
        for rec in  d.diesel.test.find({'type':'test'}):
            n += 1
        assert n == 10000, n
        print "query10 (cursor iteration) passed"
        fire('main.done', True)

    a.add_loop(Loop(mgr))
    a.add_loop(Loop(pure_db_action))
    a.add_loop(Loop(query_20_times))
    start = time.time()
    a.run()
    print "done. %.2f secs" % (time.time() - start)

Example #22
0
# vim:ts=4:sw=4:expandtab
'''Example of deferring blocking calls to threads
'''
from diesel import Application, Loop, log, thread
import time

def blocker(taskid, sleep_time):
    def task():
        while True:
            def f():
                time.sleep(sleep_time)
            thread(f)
            print 'yo!', time.time(), 'from %s task' % taskid
    return task

a = Application()
a.add_loop(Loop(blocker('fast', 1)))
a.add_loop(Loop(blocker('slow', 10)))
a.run()
Example #23
0
from diesel import Application, Service, Client, Loop, until, call, response

def handle_echo(remote_addr):
    while True:
        message = yield until('\r\n')
        yield "you said: %s" % message

class EchoClient(Client):
    @call
    def echo(self, message):
        yield message + '\r\n'
        back = yield until("\r\n")
        yield response(back)

app = Application()

def do_echos():
    client = EchoClient()
    yield client.connect('localhost', 8000)
    t = time.time()
    for x in xrange(5000):
        msg = "hello, world #%s!" % x
        echo_result = yield client.echo(msg)
        assert echo_result.strip() == "you said: %s" % msg
    print '5000 loops in %.2fs' % (time.time() - t)
    app.halt()

app.add_service(Service(handle_echo, port=8000))
app.add_loop(Loop(do_echos))
app.run()
Example #24
0
    c = RedisClient()
    sleep(1)

    print 'SEND S', time.time()

    for x in xrange(500):
        c.publish("foo", "bar")

    print 'SEND E', time.time()


hub = RedisSubHub()


def recv_loop():
    print 'RECV S', time.time()
    with hub.sub('foo') as poll:
        for x in xrange(500):
            q, content = poll.fetch()
    print 'RECV E', time.time()


a = Application()
a.add_loop(Loop(hub))  # start up the sub loop
if 'send' in sys.argv:
    a.add_loop(Loop(send_loop))
if 'recv' in sys.argv:
    a.add_loop(Loop(recv_loop))
    a.add_loop(Loop(recv_loop))
a.run()
Example #25
0
from diesel import Loop, fork, Application, sleep


def sleep_and_print(num):
    sleep(1)
    print num
    sleep(1)
    a.halt()


def forker():
    for x in xrange(5):
        fork(sleep_and_print, x)


a = Application()
a.add_loop(Loop(forker))
a.run()
Example #26
0
def free_loop():
    global free
    free += 1
    sleep(random.random())
    free -= 1
    print 'FREE', free


def sync_loop():
    global sync
    id = random.random()
    with synchronized():
        sync += 1
        sleep(random.random())
        sync -= 1
        print 'SYNC', sync


def manage():
    sleep(10)
    a.halt()


a = Application()
for l in (free_loop, sync_loop):
    for x in xrange(10):
        a.add_loop(Loop(l))
a.add_loop(Loop(manage))
a.run()
Example #27
0
            msg = thread(self.read_chat_message, "").strip()
            self.input.put(msg)

    @call
    def chat(self):
        fork(self.input_handler)
        nick = self.input.get()
        send("%s\r\n" % nick)
        while True:
            evt, data = first(until_eol=True, waits=[self.input])
            if evt == "until_eol":
                print data.strip()
            else:
                send("%s\r\n" % data)


def chat_client():
    with ChatClient('localhost', 8000) as c:
        c.chat()


app = Application()
if sys.argv[1] == "server":
    app.add_service(Service(chat_server, 8000))
elif sys.argv[1] == "client":
    app.add_loop(Loop(chat_client))
else:
    print "USAGE: python %s [server|client]" % sys.argv[0]
    raise SystemExit(1)
app.run()
Example #28
0
# vim:ts=4:sw=4:expandtab
'''Example of deferring blocking calls to threads
'''
from diesel import Application, Loop, log, thread
import time


def blocker():
    x = 1
    while True:

        def f():
            time.sleep(1)

        thread(f)
        print 'yo!', time.time()


a = Application()
a.add_loop(Loop(blocker))
a.add_loop(Loop(blocker))
a.run()
Example #29
0
            queue.get(waiting=False)
        except QueueEmpty:
            pass
        else:
            assert False

    def consumer_timeout():
        try:
            queue.get(timeout=0.1)
        except QueueTimeout:
            pass
        else:
            assert False

    def consumer(expected):
        val = queue.get()
        assert expected == val, '%s != %s' % (expected, val)

        if queue.is_empty:
            print 'success!'
            app.halt()

    app.add_loop(Loop(worker))
    app.add_loop(Loop(consumer_no_wait))
    app.add_loop(Loop(consumer_timeout))
    app.add_loop(Loop(lambda: consumer(1)))
    app.add_loop(Loop(lambda: consumer(2)))
    app.run()


Example #30
0
# vim:ts=4:sw=4:expandtab
'''Simple http client example.

Check out crawler.py for more advanced behaviors involving 
many concurrent clients.
'''

from diesel import Application, Loop, log
from diesel.protocols.http import HttpClient, HttpHeaders


def req_loop():
    with HttpClient('www.jamwt.com', 80) as client:
        heads = HttpHeaders()
        heads.set('Host', 'www.jamwt.com')
        log.info(client.request('GET', '/Py-TOC/', heads))
        log.info(client.request('GET', '/', heads))
    a.halt()


a = Application()
log = log.sublog('http-client', log.info)
a.add_loop(Loop(req_loop))
a.run()