cogen / pylons question

3 views
Skip to first unread message

Cameron

unread,
Dec 29, 2008, 11:34:08 PM12/29/08
to cogen

Hi,

I'm very new to both Cogen and Pylons. Building on the chat example,
I would like to add a coroutine that runs periodically, independent of
the web requests. I have been successful at scheduling this coroutine
via a web request, but was wondering what the best way would be to
have it start up automatically when the server starts.

Here's a summary of the code that I added so far:

1. New class Engine:

class Engine:

def __init__(self, pubsub):
self.pubsub = pubsub

@coro
def tick(self):
while 1:
# Sleep for one second
yield events.Sleep(1)

# Add a 'tick' message to the queue.
yield self.pubsub.publish("tick")

2. Init code added to the top of chat.py:

pubsub = PublishSubscribeQueue()
engine = Engine(pubsub) # New line

3. New action added to the ChatController class.

def initengine(self):
yield request.environ['cogen.core'].events.AddCoro
(engine.tick, prio=priority.CORO)

Instead of needing to call the initengine action from the browser, I
would like the engine.tick co-routine to be scheduled when the server
starts.

Any help would be appreciated.

Regards,

Cameron

Ionel Maries Cristian

unread,
Dec 30, 2008, 3:21:21 AM12/30/08
to co...@googlegroups.com
One solution I can think of is making a custom server runner in your pylons app and
doing this special initialization there.

I suggest moving all this special code in a special module in your pylons app (lets
say "async.py" for reference here).

So you would have the engine code here and the custom server runner:

class Engine:
  ....

from cogen.web.wsgi import Runner
from cogen.web.wsgi import local


pubsub = PublishSubscribeQueue()
engine = Engine(pubsub)

def server_runner(app, global_conf, host, port, **options):
  port = int(port)

  try:
    import paste.util.threadinglocal as pastelocal
    pastelocal.local = local
  except ImportError:
    pass
   
  runner = Runner(host, port, app, options)
  runner.sched.add(engine.tick)
  runner.run()


Then you would have to edit your setup.py to add a special server runner entry point:
In entry_points add:
    [paste.server_runner]
    hijacked_runner = pylonsappname.async:server_runner

And finally change the development.ini of whatever you are running paste with to have
something like:

[server:main]
use = egg:pylonsappname#hijacked_runner
--
ionel

Cameron

unread,
Dec 30, 2008, 8:35:56 AM12/30/08
to cogen

Thanks, Ionel. I'll give that a try.

Regards,

Cameron

On Dec 30, 3:21 am, "Ionel Maries Cristian" <ionel...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages