tornado 排程

https://groups.google.com/forum/#!topic/python-tornado/KEmAg97zUg8

鉴于不是所有人都能跨越GFW,摘抄如下:

 Scheduled jobs in Tornado

7 名作者发布了 9 个帖子

	Robert Wikman 	

12/2/6

将帖子翻译为中文  

Hello,



I'm trying to figure out how to write a scheduler to run jobs at

specific times, i.e. every day at 5 PM, in Tornado.

Is there any simple way to accomplish this?



Regards,

Robert



	Christopher Allick 	

12/2/7

将帖子翻译为中文  

not sure if this helps, but you could create a cron job that hits a webservice and setup a handler in your application.  you might want to protect the call.

- 隐藏引用文字 -



On Feb 6, 2012, at 9:34 AM, rbw wrote:



> Hello,

>

> I'm trying to figure out how to write a scheduler to run jobs at

> specific times, i.e. every day at 5 PM, in Tornado.

> Is there any simple way to accomplish this?

>

> Regards,

> Robert

>



	bergundy 	

12/2/7

将帖子翻译为中文  

Wasn't planning on releasing it, but what the heck :)



https://github.com/bergundy/tornado_crontab

- 隐藏引用文字 -





On Mon, Feb 6, 2012 at 7:04 PM, Christopher Allick <[email protected]> wrote:



    not sure if this helps, but you could create a cron job that hits a webservice and setup a handler in your application.  you might want to protect the call.



    On Feb 6, 2012, at 9:34 AM, rbw wrote:



    > Hello,

    >

    > I'm trying to figure out how to write a scheduler to run jobs at

    > specific times, i.e. every day at 5 PM, in Tornado.

    > Is there any simple way to accomplish this?

    >

    > Regards,

    > Robert

    >





	wataka 	

12/2/7

将帖子翻译为中文  

I use APscheduler, http://packages.python.org/APScheduler/





On Feb 6, 6:31 pm, Roey Berman <[email protected]> wrote:

> Wasn't planning on releasing it, but what the heck :)

>

> https://github.com/bergundy/tornado_crontab

>

- 显示引用文字 -

	Aleksandar Radulovic 	

12/2/7

Re: [tornado] Re: Scheduled jobs in Tornado

将帖子翻译为中文  

How about just using celery? It works like a charm and its a proven

and robust solution..



- 显示引用文字 -



-- 

a lex 13 x

http://a13x.net | @a13xnet



	Jason 	

12/2/7

将帖子翻译为中文  

I'm sorry.I don't know.

- 隐藏引用文字 -



On Feb 6, 10:34 pm, rbw <[email protected]> wrote:

> Hello,

>

> I'm trying to figure out how to write a scheduler to run jobs at

> specific times, i.e. every day at 5 PM, in Tornado.

> Is there any simple way to accomplish this?

>

> Regards,

> Robert

	Robert Wikman 	

12/2/7

将帖子翻译为中文  

Forgot to mention that I was at the time of my first post looking for

a solution using the Tornado built-in functionality, i.e.

PeriodCallback or add_timeout.



Didn't even think about using a separate module for this.



Thank you everyone.



Regards,

Robert



On Feb 6, 7:49 pm, Aleksandar Radulovic <[email protected]> wrote:

> How about just using celery? It works like a charm and its a proven

> and robust solution..

>

>

>

>

>

>

>

>

>

> On Mon, Feb 6, 2012 at 7:05 PM, wataka <[email protected]> wrote:

> > I use APscheduler,http://packages.python.org/APScheduler/

- 显示引用文字 -

	bergundy 	

12/2/8

Re: [tornado] Re: Scheduled jobs in Tornado

将帖子翻译为中文  

@wataka - Thanks for the APScheduler link, I was gonna write tests for my crontab project but now that I found out about that project I'm just using apscheduler.triggers.cron.CronTrigger



I posted a gist if anyone is interested.

http://tornadogists.org/1770500/

- 显示引用文字 -

	whitemice 	

12/2/11

Re: [tornado] Re: Scheduled jobs in Tornado

将帖子翻译为中文  

On Mon, 2012-02-06 at 10:05 -0800, wataka wrote:

> I use APscheduler, http://packages.python.org/APScheduler/



+1 APschedular



Actually I run APschedular in a process that uses AMQ to send messages

at designated times; then the apps/services just process the messages.



-- 

System & Network Administrator [ LPI & NCLA ]

<http://www.whitemiceconsulting.com>

OpenGroupware Developer <http://www.opengroupware.us>

Adam Tauno Williams

  

http://tornadogists.com/1770500/

import re

import itertools

import logging

 

from apscheduler.triggers.cron import CronTrigger

from tornado.ioloop import IOLoop

from datetime import datetime

 

class CronCallback(object):

    """Schedules the given callback to be called periodically.



    The callback is called according to the schedule argument.



    `start` must be called after the CronCallback is created.



    If schedule is a string it should contain 7 cron fields: ('second', 'minute', 'hour', 'day', 'month', 'year', 'day_of_week').

    If schedule is a dict it must contain at least one of the fields above.



    >>> cron1 = CronCallback(lambda: logging.error('x'), dict(seconds = 1)) # logs 'x' every second

    >>> cron2 = CronCallback(lambda: IOLoop.instance().stop(), '*/5 * * * * * *') # stops ioloop every 5 seconds

    >>> cron1.start()

    >>> cron2.start()

    >>> IOLoop.instance().start()

    """

 

    _split_re  = re.compile("\s+")

    _sched_seq = ('second', 'minute', 'hour', 'day', 'month', 'year', 'day_of_week')

 

    def __init__(self, callback, schedule, io_loop=None):

        if isinstance(schedule, basestring):

            splitted = self._split_re.split(schedule)

            if len(splitted) < 7:

                raise TypeError("'schedule' argument pattern mismatch")

 

            schedule = dict(itertools.izip(self._sched_seq, splitted))

        

        self.callback = callback

        self._trigger = CronTrigger(**schedule)

        self.io_loop  = io_loop or IOLoop.instance()

        self._running = False

        self._timeout = None

 

    def start(self):

        """Starts the timer."""

        self._running = True

        self._schedule_next()

 

    def stop(self):

        """Stops the timer."""

        self._running = False

        if self._timeout is not None:

            self.io_loop.remove_timeout(self._timeout)

            self._timeout = None

 

    def _run(self):

        if not self._running: return

        try:

            self.callback()

        except Exception:

            logging.error("Error in cron callback", exc_info=True)

        self._schedule_next()

 

    def _schedule_next(self):

        if self._running:

            self._timeout = self.io_loop.add_timeout(self._next_timeout, self._run)

 

    @property

    def _next_timeout(self):

        d = datetime.now()

        return self._trigger.get_next_fire_time(d) - d

 

if __name__ == "__main__":

    import doctest

    doctest.testmod()

  

你可能感兴趣的:(tornado)