Ejemplo n.º 1
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()
Ejemplo n.º 2
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()
Ejemplo n.º 3
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()
Ejemplo n.º 4
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()
Ejemplo n.º 5
0
        message = until('\r\n')
        send("you said: %s" % message)

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

app = Application()
log = log.sublog('echo-system', log.info)

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

server_ctx = SSL.Context(SSL.TLSv1_METHOD)
server_ctx.use_privatekey_file('snakeoil-key.pem')
server_ctx.use_certificate_file('snakeoil-cert.pem')
app.add_service(Service(handle_echo, port=8000, ssl_ctx=server_ctx))

app.add_loop(Loop(do_echos))
app.run()
Ejemplo n.º 6
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()
Ejemplo n.º 7
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()
Ejemplo n.º 8
0
from diesel import Application, Loop, sleep
import time


def l():
    for x in xrange(2):
        print "hi"
        time.sleep(1)
        sleep(5)
    a.halt()


a = Application()
a.add_loop(Loop(l))
a.add_loop(Loop(l))
a.run()
Ejemplo n.º 9
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()
Ejemplo n.º 10
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()
Ejemplo n.º 11
0
    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()
Ejemplo n.º 12
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()
Ejemplo n.º 13
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)

Ejemplo n.º 14
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()
Ejemplo n.º 15
0
            assert 0, "DID NOT RAISE"

        # Try watching keys in a transaction.
        r.set('w1', 'watch me')
        transaction = r.transaction(watch=['w1'])
        w1 = r.get('w1')
        with transaction:
            transaction.set('w2', w1 + ' if you can!')
        assert transaction.value == ['OK']
        assert r.get('w2') == 'watch me if you can!'

        # Try changing watched keys.
        r.set('w1', 'watch me')
        transaction = r.transaction(watch=['w1'])
        r.set('w1', 'changed!')
        w1 = r.get('w1')
        try:
            with transaction:
                transaction.set('w2', w1 + ' if you can!')
        except RedisTransactionError:
            assert transaction.aborted
        else:
            assert 0, "DID NOT RAISE"

        print 'all tests pass.'

        a.halt()

    a.add_loop(Loop(do_set))
    a.run()
Ejemplo n.º 16
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()
Ejemplo n.º 17
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, track=self.track)
Ejemplo n.º 18
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()


Ejemplo n.º 19
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()
Ejemplo n.º 20
0
            if q == inq:  # getting message from client
                print "(inq) %s" % v
                cmd = v.get("cmd", "")
                if cmd == "":
                    print "published message to %i subscribers" % c.publish(
                        "foo",
                        dumps({
                            'nick': cgi.escape(v['nick'].strip()),
                            'message': cgi.escape(v['message'].strip()),
                        }))
                else:
                    outq.put(dict(message="test bot"))
            elif q == group:  # getting message for broadcasting
                chan, msg_str = v
                try:
                    msg = loads(msg_str)
                    data = dict(message=msg['message'], nick=msg['nick'])
                    print "(outq) %s" % data
                    outq.put(data)
                except JSONDecodeError:
                    print "error decoding message %s" % msg_str
            elif isinstance(
                    v, WebSocketDisconnect):  # getting a disconnect signal
                return
            else:
                print "oops %s" % v


app.diesel_app.add_loop(Loop(hub))
app.run()