how to call a function for evry 10 secs

23 views
Skip to first unread message

hisan

unread,
Jun 29, 2011, 1:40:34 PM6/29/11
to pylons-...@googlegroups.com
how to call a function for evry 10 secs in python

Mike Orr

unread,
Jun 29, 2011, 5:30:52 PM6/29/11
to pylons-...@googlegroups.com
On Wed, Jun 29, 2011 at 10:40 AM, hisan <santos...@gmail.com> wrote:
> how to call a function for evry 10 secs in python

I assume this is a Pylons or Pyramid application since this is
pylons-discuss. The easiest way would probably be to start a thread in
the initialization code (environment.py for Pylons, main() for
Pyramid). The thread would run a long-running function with a loop
that that records its start time, does its thing, sleeps for ``(10
seconds - (now - start_time))``, and repeats.

Another way to do repeated events is with cron, possibly using "paster
request". But that won't work in this case because cron can't handle
intervals of less than a minute.

--
Mike Orr <slugg...@gmail.com>

jerry

unread,
Jun 29, 2011, 10:11:07 PM6/29/11
to pylons-discuss
Hi Mike,

I'm curious to know where is this "main() for Pyramid" you were
referring to?

Thanks.

Jerry

On Jun 30, 5:30 am, Mike Orr <sluggos...@gmail.com> wrote:
> Mike Orr <sluggos...@gmail.com>

Mike Orr

unread,
Jun 30, 2011, 7:01:38 PM6/30/11
to pylons-...@googlegroups.com
It's the main() function in your application's top-level
myapp/__init__.py module.

> --
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
>
>

--
Mike Orr <slugg...@gmail.com>

Stéphane Klein

unread,
Jul 3, 2011, 9:10:09 AM7/3/11
to pylons-...@googlegroups.com
Le 29/06/2011 23:30, Mike Orr a �crit :

Celery library can't help to do this task ?

http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html

Regards,
Stephane

--
St�phane Klein <step...@harobed.org>
blog: http://stephane-klein.info
Twitter: http://twitter.com/klein_stephane
pro: http://www.is-webdesign.com

Roberto De Ioris

unread,
Jul 3, 2011, 2:00:26 PM7/3/11
to pylons-...@googlegroups.com

> how to call a function for evry 10 secs in python

I do not know if changing deploy/hosting platform is a viable choice, but
uWSGI includes a huge set of (very cheap) functionality to allows this
sort of things

(elegant way)
http://projects.unbit.it/uwsgi/wiki/Decorators
(low-level way)
http://projects.unbit.it/uwsgi/wiki/SignalFramework

For example you can schedule a timer function simply doing

from uwsgidecorators import *

@timer(10)
def every_10_seconds(signum):
print("hello World")

--
Roberto De Ioris
http://unbit.it

Mike Orr

unread,
Jul 3, 2011, 1:48:52 PM7/3/11
to pylons-...@googlegroups.com
On Sun, Jul 3, 2011 at 6:10 AM, Stéphane Klein <step...@harobed.org> wrote:

> Le 29/06/2011 23:30, Mike Orr a écrit :
>>
>> On Wed, Jun 29, 2011 at 10:40 AM, hisan<santos...@gmail.com>  wrote:
>>>
>>> how to call a function for evry 10 secs in python
>>
>> I assume this is a Pylons or Pyramid application since this is
>> pylons-discuss. The easiest way would probably be to start a thread in
>> the initialization code (environment.py for Pylons, main() for
>> Pyramid). The thread would run a long-running function with a loop
>> that that records its start time, does its thing, sleeps for ``(10
>> seconds - (now - start_time))``, and repeats.
>>
>> Another way to do repeated events is with cron, possibly using "paster
>> request". But that won't work in this case because cron can't handle
>> intervals of less than a minute.
>>
>
> Celery library can't help to do this task ?
>
> http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html

I don't know much about Celery or parallel programming, but it seems
like more software than necessary for a single repeated task. Celery
is more the kind of thing you'd use to replace the main thread pool
with (i.e., in paste.httpserver) -- where you have several different
kinds of messages coming simultaneously from unknown sources. If
you're just repeating a single task, it would be overkill. Also note
that Celery is asynchronous. In order to integrate into a
mulththreaded program (as opposed to replacing the thread pool with an
asynchronous non-pool), you'd have to run it in a separate thread (in
which case my suggestion would be easier), or run the other task (the
one it's sharing the thread with) in Celery too, which would be
difficult to retrofit into an existing threaded application.

--
Mike Orr <slugg...@gmail.com>

Reply all
Reply to author
Forward
0 new messages