Exemplo n.º 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()
Exemplo n.º 2
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()
Exemplo n.º 3
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()
Exemplo n.º 4
0
    sleep(10)

    log.info("putting 50000 *more* things on log")
    for x in xrange(50000, 100000):
        q.put(x)
        sleep()

def getter():
    log = glog.sublog("getter", glog.info)
    got = 0
    while got < 100000:
        try:
            s = q.get(timeout=3)
        except QueueTimeout:
            log.warn("timeout before getting a value, retrying...")
            continue
        assert s == got
        got += 1

        if got % 10000 == 0:
            log.info("up to %s received, sleeping for 0.5s" % got)
            sleep(0.5)

    log.info("SUCCESS!  got all 100,000")
    a.halt()

a = Application()
a.add_loop(Loop(putter))
a.add_loop(Loop(getter))
a.run()
Exemplo n.º 5
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()
Exemplo n.º 6
0
import time, sys

def send_loop():
    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()
Exemplo n.º 7
0
        db = PostgresClient()
        db.connect(user="******", password="******", database="test")
        t = time.time()
        for x in xrange(5000):
            db.simplequery("select userid, fakenum from testtable where groupid='pgtest' limit 5")
        print time.time() - t

    def pgtest():
        db = PostgresClient()
        db.connect(user="******", password="******", database="test")
        db.simplequery("select * from testtable limit 2")
        db.simplequery("insert into testtable values ('pgtest', 'pgtest', 5500)")
        db.simplequery("insert into testtable values ('10', 'pgtest', 5500)")
        print db.simplequery("select * from testtable where groupid='10'")
        db.simplequery("update testtable set fakenum=8800 where groupid='pgtest'")
        db.simplequery("select userid, fakenum from testtable where groupid='pgtest' limit 5")
        db.extquery_prepare("select userid, fakenum from testtable where groupid=$1 limit 5", "foobar")
        print db.extquery(("pgtest",), "foobar")
        print db.extquery_dict(("pgtest",), "foobar")
        print db.simplequery("select userid, fakenum from testtable where groupid='pgtest' limit 5")
        print db.simplequery_dict("select userid, fakenum from testtable where groupid='pgtest' limit 5")

    a.add_loop(Loop(pgtest))
    #    a.add_loop(Loop(exttest_time))
    #    a.add_loop(Loop(exttest_time))
    #    a.add_loop(Loop(exttest_time))
    #    a.add_loop(Loop(exttest_time))
    #    a.add_loop(Loop(exttest_time))
    #    a.add_loop(Loop(exttest_time))
    a.run()
Exemplo n.º 8
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()
Exemplo n.º 9
0
        while 1:
            bar = client.echo("foo %s" % n)
            tms = time.asctime()
            log.info("[%s] %s: remote service said %r" % (tms, n, bar))
            sleep(2)

    return _loop


def echo_self_loop(n):
    def _loop():
        client = EchoClient('localhost', 8013)
        while 1:
            bar = client.echo_whatup()
            tms = time.asctime()
            log.info("[%s] %s: (whatup) remote service said %r" %
                     (tms, n, bar))
            sleep(3)

    return _loop


a = Application()
log = log.sublog('echo-client', log.info)

for x in xrange(5):
    a.add_loop(Loop(echo_loop(x)))
for x in xrange(5):
    a.add_loop(Loop(echo_self_loop(x)))
a.run()
Exemplo n.º 10
0
        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()
Exemplo n.º 11
0

        print (r.zrange("z1", 0, -1))
        print (r.zrem("z1", "two"))
        print (r.zrange("z1", 0, -1))
        print (r.zrevrange("z1", 0, -1))

        print (r.zrem("z1", (r.zrange("z1", 0, 0))[0]))
        print (r.zrange("z1", 0, -1))
        print (r.zcard("z1"))

        print 'done!'

        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):
        client = RedisClient(self.host, self.port)
Exemplo n.º 12
0
                    client, heads = get_client()
                code, heads, body = client.request('GET', lpath, heads)
            except ConnectionClosed:
                pass
            else:
                write_file(lpath, body)
                files +=1
                break
    
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))

a = Application()
a.add_loop(Loop(req_loop))

log = log.sublog('http-crawler', log.info)

def stop():
    log.info("Fetched %s files in %.3fs with concurrency=%s" % (files, time.time() - t, CONCURRENCY))
    a.halt() # stop application

t = time.time()
a.run()
Exemplo n.º 13
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()


Exemplo n.º 14
0
def resolve_the_google():
    print "started resolution!"
    g_ip = resolve_dns_name("www.google.com")
    print "www.google.com's ip is %s" % g_ip
    try:
        bad_host = "www.g8asdf21oogle.com"
        print "now checking %s" % bad_host
        resolve_dns_name(bad_host)
    except DNSResolutionError:
        print "yep, it failed as expected"
    else:
        raise RuntimeError("The bad host resolved.  That's unexpected.")
    g_ip = resolve_dns_name("www.google.com")
    g_ip = resolve_dns_name("www.google.com")
    g_ip = resolve_dns_name("www.google.com")
    g_ip = resolve_dns_name("www.google.com")
    a.halt()


def stuff():
    while True:
        print "doing stuff!"
        sleep(0.01)


a = Application()
a.add_loop(Loop(stuff))
a.add_loop(Loop(resolve_the_google))
a.run()
Exemplo n.º 15
0
import os
from diesel import Loop, fork, Application, sleep
from diesel.util.stats import CPUStats

def not_always_busy_worker():
    with CPUStats() as stats:
        for _ in xrange(12):
            for i in xrange(10000000): # do some work to forward cpu seconds
                pass
            sleep(0.1) # give up control

    print "cpu seconds ",  stats.cpu_seconds

def spawn_busy_workers():
    for _ in xrange(0,3):
        fork(not_always_busy_worker)

a = Application()
a.add_loop(Loop(spawn_busy_workers), track=True)
a.run()
Exemplo n.º 16
0
# vim:ts=4:sw=4:expandtab
'''Simple udp echo client.
'''
import time
from diesel import Application, UDPService, UDPLoop, send, sleep

def hi_loop():
    while 1:
        send("whatup?", addr='localhost', port=8013)
        print time.ctime(), "sent message to server"
        sleep(3)

def hi_client(data, addr):
    print time.ctime(), "remote service said '%s'" % data

app = Application()
app.add_service(UDPService(hi_client, 8014))
app.add_loop(UDPLoop(hi_loop))
app.run()
Exemplo n.º 17
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)

Exemplo n.º 18
0
# vim:ts=4:sw=4:expandtab
'''Example of event firing.
'''
import time
import random
from diesel import Application, Loop, sleep, fire, wait, log

def gunner():
    x = 1
    while True:
        fire('bam', x)
        x += 1
        sleep()

def sieged():
    t = time.time()
    while True:
        n = wait('bam')
        if n % 10000 == 0:
            log.info(n)
            if n == 50000:
                delt = time.time() - t
                log.info("50,000 messages in %.3fs (%.1f/s)" % (delt, 50000 / delt))
                a.halt()

a = Application()
log = log.sublog('fire-system', log.info)
a.add_loop(Loop(gunner))
a.add_loop(Loop(sieged))
a.run()
Exemplo n.º 19
0
from diesel import Application, Loop, sleep
import time

def restart():
    print "I should restart"
    a = b

a = Application()
a.add_loop(Loop(restart), keep_alive=True)
a.run()
Exemplo n.º 20
0
        yield sleep(8)
        yield c.sub.test.update({'name':'allrooms'}, {'name':'allrooms', 'value':['foo', 'bar', 'baz']}, upsert=1)

    def wait_for_doc_update(req):
        c = SubscribingClient(id='foo-sub')
        yield c.connect(BACKEND_HOST, FRONTEND_PORT)
        yield c.bub.foo.subscribe({'junk':'no'})
        val = str((yield c.bub.foo.wait()))
        headers = http.HttpHeaders()
        headers.add('Content-Length', len(val))
        headers.add('Content-Type', 'text/plain')
        yield http.http_response(req, 200, headers, val)

    def main():
        c = MongoClient()
        yield c.connect(BACKEND_HOST, FRONTEND_PORT)
        yield c.drop_database('sub')
        yield c.drop_database('bub')
        print "main: dropped the db"
        a.add_loop(Loop(subscriber))
        a.add_loop(Loop(publisher))
        print "main: loops started"
        c.close()

    a = Application()
    a.add_service(Service(SubscriptionProxy(BACKEND_HOST, BACKEND_PORT), FRONTEND_PORT))
    a.add_service(Service(http.HttpServer(wait_for_doc_update), 8088))
    a.add_loop(Loop(main))
    a.run()

Exemplo n.º 21
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()
Exemplo n.º 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():
    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()
Exemplo n.º 23
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)

Exemplo n.º 24
0
        client = EchoClient("localhost", 8013)
        while 1:
            bar = client.echo("foo %s" % n)
            tms = time.asctime()
            log.info("[%s] %s: remote service said %r" % (tms, n, bar))
            sleep(2)

    return _loop


def echo_self_loop(n):
    def _loop():
        client = EchoClient("localhost", 8013)
        while 1:
            bar = client.echo_whatup()
            tms = time.asctime()
            log.info("[%s] %s: (whatup) remote service said %r" % (tms, n, bar))
            sleep(3)

    return _loop


a = Application()
log = log.sublog("echo-client", log.info)

for x in xrange(5):
    a.add_loop(Loop(echo_loop(x)))
for x in xrange(5):
    a.add_loop(Loop(echo_self_loop(x)))
a.run()
Exemplo n.º 25
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()
Exemplo n.º 26
0
def actor():
    while True:
        board = yield wait('board-received')
        #print "got the board..."
        yield fire('move-made', tron.move(random_move(board)))

def random_move(board):
    move = random.choice(board.moves())
    #log.critical("made decision in: %.6fs" % (time.time()-t))
    return move


if __name__=="__main__":
    app = Application()
    app.add_loop(Loop(actor))
    app.add_loop(Loop(getboard))
    app.run()
    
#     
# def bestmove(board):
#     log.critical("received board %s" % board)
#     bestcount = -1
#     bestmove = tron.NORTH
#     for dir in board.moves:
#         dest = board.rel(dir)
#         count = 0
#         for pos in board.adjacent(dest):
#             if board[pos] == tron.FLOOR:
#                 count += 1
#         if count > bestcount: