Ejemplo n.º 1
0
 def __init__(self, wsgi_callable, port=80, iface=''):
     Application.__init__(self)
     self.port = port
     self.wsgi_callable = wsgi_callable
     http_service = Service(
         HttpServer(WSGIRequestHandler(wsgi_callable, port)), port, iface)
     self.add_service(http_service)
Ejemplo n.º 2
0
Archivo: cli.py Proyecto: 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)
Ejemplo 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()
Ejemplo n.º 4
0
class DieselTest(object):
    def setup_method(self, *args):
        self._app = Application(allow_app_replacement=True)
        self._trigger = TestTrigger()

    # XXX py.test magic args?
    def prepare_test(self):
        return self._app, self._trigger.touch, TestAccumulator()

    def run_test(self, count=1, timeout=10):
        def trigger_thread():
            self._trigger.wait(timeout, count)
            try:
                self._app.halt()
            except app.ApplicationEnd:
                # XXX Does halt have to raise this? Should we do anything but
                # pass?
                pass
            self._app.hub.wake_from_other_thread()

        thread.start_new_thread(trigger_thread, ())
        self._app.run()
        if self._trigger.timed_out:
            raise TestTimeout()

    def teardown_method(self, *args):
        try:
            self._app.halt()
        except app.ApplicationEnd:
            # This is always raised?
            pass
        self._app = self._trigger = None
Ejemplo n.º 5
0
class DieselTest(object):
    def setup_method(self, *args):
        self._app = Application(allow_app_replacement=True)
        self._trigger = TestTrigger()

    # XXX py.test magic args?
    def prepare_test(self):
        return self._app, self._trigger.touch, TestAccumulator()

    def run_test(self, count=1, timeout=10):
        def trigger_thread():
            self._trigger.wait(timeout, count)
            try:
                self._app.halt()
            except app.ApplicationEnd:
                # XXX Does halt have to raise this? Should we do anything but
                # pass?
                pass
            self._app.hub.wake_from_other_thread()

        thread.start_new_thread(trigger_thread, ())
        self._app.run()
        if self._trigger.timed_out:
            raise TestTimeout()

    def teardown_method(self, *args):
        try:
            self._app.halt()
        except app.ApplicationEnd:
            # This is always raised?
            pass
        self._app = self._trigger = None
Ejemplo n.º 6
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()
Ejemplo n.º 7
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()
Ejemplo n.º 8
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()
Ejemplo n.º 9
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()
Ejemplo n.º 10
0
class DieselTest(object):
    def setup_method(self, *args):
        self._app = Application(allow_app_replacement=True)
        self._trigger = TestTrigger()

    # XXX py.test magic args?
    def prepare_test(self):
        return self._app, self._trigger.touch, TestAccumulator()

    def run_test(self, count=1, timeout=10):
        def trigger_thread():
            self._trigger.wait(timeout, count)
            self._app.halt()
            self._app.hub.wake_from_other_thread()
            
        thread.start_new_thread(trigger_thread, ())
        self._app.run()
        if self._trigger.timed_out:
            raise TestTimeout()

    def teardown_method(self, *args):
        self._app.halt()
        self._app = self._trigger = None
Ejemplo n.º 11
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.º 12
0
# vim:ts=4:sw=4:expandtab
'''Simple echo server.
'''
from diesel import Application, Service, until_eol, send

def hi_server(addr):
    while 1:
        inp = until_eol()
        if inp.strip() == "quit":
            break
        send("you said %s" % inp)

app = Application()
app.add_service(Service(hi_server, 8013))
app.run()
Ejemplo n.º 13
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.º 14
0
        send(response)

    def from_backend(self, data, respond, backend=None):
        if not backend:
            backend = self.ClientClass()
            backend.connect(self.backend_host, self.backend_port)
        resp = backend.send(data, respond)
        return (backend, resp)

if __name__ == '__main__':
    import time
    from pprint import pprint
    from diesel import fire, wait
    HOST = 'localhost'
    PORT = 27017
    a = Application()

    def mgr():
        _ = first(waits=['main.done', 'queries.done'])
        _ = first(waits=['main.done', 'queries.done'])
        a.halt()

    def query_20_times():
        d = MongoClient(HOST, PORT)
        counts = []
        wait('deleted')
        for i in range(20):
            with d.diesel.test.find({'type':'test'}, limit=500) as cursor:
                while not cursor.finished:
                    counts.append(len(cursor.more()))
            if not i:
Ejemplo n.º 15
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.º 16
0

def is_backend_message(cls):
    try:
        return issubclass(cls, PostgresMessage) and not cls.frontend_only
    except:
        return False


MessageTypes = dict((c.idbyte, c) for k, c in locals().iteritems() if is_backend_message(c))

if __name__ == "__main__":
    from diesel import Application, Loop
    import time

    a = Application()

    def exttest_time():
        db = PostgresClient()
        db.connect(user="******", password="******", database="test")
        db.extquery_prepare("select userid, fakenum from testtable where groupid=$1 limit 5", "foobar")
        t = time.time()
        for x in xrange(5000):
            db.extquery(("pgtest",), "foobar")
        print time.time() - t

    def simpletest_time():
        db = PostgresClient()
        db.connect(user="******", password="******", database="test")
        t = time.time()
        for x in xrange(5000):
Ejemplo n.º 17
0
# vim:ts=4:sw=4:expandtab
'''The oh-so-canonical "Hello, World!" http server.
'''
from diesel import Application, Service
from diesel.protocols import http


def hello_http(req):
    return http.Response("Hello, World!")


app = Application()
app.add_service(Service(http.HttpServer(hello_http), 8088))
import cProfile
cProfile.run('app.run()')
Ejemplo n.º 18
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()
Ejemplo n.º 19
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.º 20
0
 def setup_method(self, *args):
     self._app = Application(allow_app_replacement=True)
     self._trigger = TestTrigger()
Ejemplo n.º 21
0
                wait(self.wait_id)

        if self.inp:
            return self.inp.popleft()
        elif not waiting:
            raise QueueEmpty()


    @property
    def is_empty(self):
        return not bool(self.inp)

if __name__ == '__main__':
    from diesel import Application, Loop, sleep

    app = Application()

    queue = Queue()

    def worker():
        sleep(0.25)

        queue.put(1)
        queue.put(2)

    def consumer_no_wait():
        try:
            queue.get(waiting=False)
        except QueueEmpty:
            pass
        else:
Ejemplo 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(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.º 23
0
# Length of message contents in bytes, including self.
# 

def is_backend_message(cls):
    try:
        return issubclass(cls, PostgresMessage) and not cls.frontend_only
    except:
        return False

MessageTypes = dict((c.idbyte, c) for k,c in locals().iteritems() if is_backend_message(c))

if __name__ == '__main__':
    from diesel import Application, Loop
    import time

    a = Application()

    def exttest_time():
        db = PostgresClient()
        db.connect(user='******', password='******', database='test')
        db.extquery_prepare('select userid, fakenum from testtable where groupid=$1 limit 5', 'foobar')
        t = time.time()
        for x in xrange(5000):
            db.extquery(('pgtest',), 'foobar')
        print time.time() - t

    def simpletest_time():
        db = PostgresClient()
        db.connect(user='******', password='******', database='test')
        t = time.time()
        for x in xrange(5000):
Ejemplo n.º 24
0
Archivo: http.py Proyecto: dowski/aspen
# vim:ts=4:sw=4:expandtab
'''The oh-so-canonical "Hello, World!" http server.
'''
from diesel import Application, Service
from diesel.protocols import http

# Pre-gen, since it's static.. 
content = "Hello, World!"
headers = http.HttpHeaders()
headers.add('Content-Length', len(content))
headers.add('Content-Type', 'text/plain')

def hello_http(req):
    return http.http_response(req, 200, headers, content)

app = Application()
app.add_service(Service(http.HttpServer(hello_http), 8088))
app.run()
Ejemplo n.º 25
0
# vim:ts=4:sw=4:expandtab
'''Simple echo server.
'''
from diesel import Application, Service, until_eol, send


def hi_server(addr):
    while 1:
        inp = until_eol()
        if inp.strip() == "quit":
            break
        send("you said %s" % inp)


app = Application()
app.add_service(Service(hi_server, 8013))
app.run()
Ejemplo n.º 26
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.º 27
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.º 28
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()
Ejemplo n.º 29
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.º 30
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()
Ejemplo n.º 31
0
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))
Ejemplo n.º 32
0
<input type="text" size="40" id="the-i" /> <input type="button" value="Update Message" onclick="push(); return false" />

</body>
</html>
''' % LOCATION

def web_handler(req):
    heads = HttpHeaders()
    heads.add('Content-Length', len(content))
    heads.add('Content-Type', 'text/html')

    return http_response(req, 200, heads, content)

import time

def socket_handler(req, inq, outq):
    message = "hello, there!"
    while True:
        try:
            v = inq.get(timeout=0.5)
        except QueueTimeout:
            pass
        else:
            message = v['message']

        outq.put(WSD(message=message, time=time.time()))

a = Application()
a.add_service(Service(WebSocketServer(web_handler, socket_handler, LOCATION), 8091))
a.run()
Ejemplo n.º 33
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.º 34
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.º 35
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.º 36
0
            raise QueueEmpty()

    def __iter__(self):
        return self

    def next(self):
        return self.get()

    @property
    def is_empty(self):
        return not bool(self.inp)

if __name__ == '__main__':
    from diesel import Application, Loop, sleep

    app = Application()

    queue = Queue()

    def worker():
        sleep(0.25)

        queue.put(1)
        queue.put(2)

    def consumer_no_wait():
        try:
            queue.get(waiting=False)
        except QueueEmpty:
            pass
        else:
Ejemplo n.º 37
0
 def setup_method(self, *args):
     self._app = Application(allow_app_replacement=True)
     self._trigger = TestTrigger()
Ejemplo n.º 38
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()
Ejemplo n.º 39
0
from diesel import Application, Service, Client, Loop, send
from diesel import until, call, log

def handle_echo(remote_addr):
    while True:
        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')
Ejemplo n.º 40
0
# vim:ts=4:sw=4:expandtab
'''Demonstrate sleep-type behavior server-side.
'''
from diesel import Application, Service, until_eol, sleep, send

def delay_echo_server(addr):
    inp = until_eol()

    for x in xrange(4):
        sleep(2)
        send(str(x) + '\r\n')
    send("you said %s" % inp)

app = Application()
app.add_service(Service(delay_echo_server, 8013))
app.run()
Ejemplo n.º 41
0
#!/usr/bin/env python
from diesel import Application, Service, until_eol, fire, wait

def chat_server(addr):
    my_nick = (yield until_eol()).strip()
    while True:
        my_message, other_message = yield (until_eol(), wait('chat_message'))
        if my_message:
            yield fire('chat_message', (my_nick, my_message.strip()))
        else:
            nick, message = other_message
            yield "<%s> %s\r\n"  % (nick, message)

app = Application()
app.add_service(Service(chat_server, 8000))
app.run()
Ejemplo n.º 42
0
                    else:
                        resp.append(receive(l))
                        until_eol() # noop
                elif hl[0] == ':':
                    resp.append(int(hl[1:]))
            return resp
        elif c == ':':
            return int(fl[1:])
        elif c == '-':
            e_message = fl[1:]
            raise RedisError(e_message)

if __name__ == '__main__':
    from diesel import Application, Loop

    a = Application()

    def do_set():
        r = RedisClient()

        r.set('foo3', 'bar')
        assert r.exists('foo3')
        r.delete('foo3')
        assert not r.exists('foo3')

        for x in xrange(5000):
            r.set('foo', 'bar')

        print (r.get('foo'))
        print (r.get('foo2'))
        print (r.exists('foo'))
Ejemplo n.º 43
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.º 44
0
import time
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()
Ejemplo n.º 45
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()

Ejemplo n.º 46
0
 def __init__(self, wsgi_callable, port=80, iface=''):
     Application.__init__(self)
     self.port = port
     self.wsgi_callable = wsgi_callable
     http_service = Service(HttpServer(WSGIRequestHandler(wsgi_callable, port)), port, iface)
     self.add_service(http_service)
Ejemplo n.º 47
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.º 48
0
Archivo: ws.py Proyecto: dowski/aspen
<input type="text" size="40" id="the-i" /> <input type="button" value="Update Message" onclick="push(); return false" />

</body>
</html>
''' % LOCATION

def web_handler(req):
    heads = HttpHeaders()
    heads.add('Content-Length', len(content))
    heads.add('Content-Type', 'text/html')

    return http_response(req, 200, heads, content)

import time

def socket_handler(inq, outq):
    message = "hello, there!"
    while True:
        try:
            v = inq.get(timeout=0.5)
        except QueueTimeout:
            pass
        else:
            message = v['message']
                        
        outq.put(WSD(message=message, time=time.time()))

a = Application()
a.add_service(Service(WebSocketServer(web_handler, socket_handler, LOCATION), 8091))
a.run()
Ejemplo n.º 49
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.º 50
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.º 51
0
# vim:ts=4:sw=4:expandtab
'''Non blocking memcache get/set.
'''
from diesel import Application, Service
from diesel.protocols.memcache import MemCacheClient
from diesel.protocols import http
from diesel import log

def delay_echo_server(addr):
    # default to 127.0.0.1:11211
    m = MemCacheClient('localhost')
    value = m.get('mykey')
    values = m.get_multi(['mykey', 'mykey1', 'mykey2'])
    
    return http.Response("value from multi_get : %s and get : %s"%(str(values), str(value)))
    #return http.Response("value from memcache key : %s key1 : %s and key 2 : %s"%(str(value), str(value1), str(value2)))

app = Application()
app.add_service(Service(http.HttpServer(delay_echo_server), 8000))
app.run()
Ejemplo n.º 52
0
                        resp.append(receive(l))
                        until_eol()  # noop
                elif hl[0] == ':':
                    resp.append(int(hl[1:]))
            return resp
        elif c == ':':
            return int(fl[1:])
        elif c == '-':
            e_message = fl[1:]
            raise RedisError(e_message)


if __name__ == '__main__':
    from diesel import Application, Loop

    a = Application()

    def do_set():
        r = RedisClient()

        r.select(11)
        r.flushdb()

        print '--BASIC--'
        assert r.get('newdb') == None
        r.set('newdb', '1')

        r.set('foo3', 'bar')
        assert r.exists('foo3')
        r.delete('foo3')
        assert not r.exists('foo3')
Ejemplo n.º 53
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()