[ create a new paste ] login | about

Link: http://codepad.org/gO66UDby    [ raw code | fork ]

Python, pasted on Dec 19:
#!/usr/bin/env python

import os

from circuits.web import Server, Controller

# NOTE task is a PEP8 violation
# XXX: Yes I know :) But there's a very good reason for it!
from circuits.core import Worker, task, Debugger

def complicated_task(*args, **kwargs):
    """This is so complex I need a worker!"""

    print(args, kwargs)

    import time

    return 'Task worker ' + str(os.getpid())

class Root(Controller):
    """The root of the application"""

    def index(self):
        """The index"""

        return 'Hello World!'

    def redirect_target(self):
        """Just a normal url handler"""

        return "Hello, you redirected to me?"

    def redirecter(self):
        """This is how a redirect works"""

        return self.redirect('/redirect_target')

    def bad_redirecter(self):
        """This is always fun!"""

        return self.redirect('/bad_redirecter')

    def sleeper(self):
        """Timer that blocks this loop, let's you see other event loops,
        pids.  Otherwise the event loop is faster then we can request
        through the browser.

        import time

        return str(os.getpid())

    def sleep_worker(self):
        """A Worker"""

        result = yield self.call(task(complicated_task), 'worker')

        yield result

        # XXX: return is invalid inside a generator
        #return result

        # print(next(result))
        # value_obj = next(result)
        # print(value_obj.value)

        # if result:
            # return 'Success'
        # else:
            # return 'Failure'

app_server = Server(("", 9000))
(Root() + Debugger() + Worker(process=True)).register(app_server)

# NOTE You must register before starting additional
# procesesses, not sure why.
# XXX: Because registering creates a connected component graph
# XXX: Without registering you'd start uesless (almost blank) processes

# XXX: COuld get ugly/messy here... Might not work so well with multiprocessing.Pool/circuits.Worker combo.


Create a new paste based on this one