Scheduling Events

This is the place for queries that don't fit in any of the other categories.

Scheduling Events

Postby jogl8 » Tue Feb 19, 2013 6:05 pm

I am collecting data form various devices. I have it set up so that I poll the devices and then they respond with data via the LAN. Some get polled every 5s, some every minute, some hourly, etc.

I will set the polling routines up as functions.

What are the (easy) options for calling functions on timed intervals?

Thanks,
JP.
jogl8
 
Posts: 26
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: Scheduling Events

Postby zeycus » Tue Feb 19, 2013 9:43 pm

I am not sure it is the easiest way, but you could take a look at the sched module.
I used it to schedule the call of functions, I think you can use both relative and absolute
trigger times.
Image

Live long and prosper.
Spock
User avatar
zeycus
 
Posts: 23
Joined: Sun Feb 17, 2013 10:30 am
Location: Madrid

Re: Scheduling Events

Postby kamiyl » Tue Feb 19, 2013 9:47 pm

Hello JP,

In this case, I think you may want to use a multiple-threading module like threading, in junction with the time.sleep method to regulate the intervals between function returns.

Here's a very basic implementation:
Code: Select all
import time
import threading

def Foo(): # a function to execute every 2 seconds
    for x in range(0, 10):
        print "Foo:", x
        time.sleep(2) # makes execution stop for 2 seconds

def Bar(): # a function to execute every 4 seconds
    for x in range(0, 10):
        print "Bar:", x*2
        time.sleep(4) # makes execution stop for 4 seconds

thread_Foo = threading.Thread(None, Foo, None, (), {}) # creates a <Thread> instance to represent the process
thread_Bar = threading.Thread(None, Bar, None, (), {}) # idem

thread_Foo.start() # starts the thread
thread_Bar.start() # starts the other thread


I recommend that you look at the Python documentation because it is much better at explaining than I am. :P

I hope I have been helpful,
Kam
kamiyl
 
Posts: 2
Joined: Sun Feb 17, 2013 5:56 pm

Re: Scheduling Events

Postby jogl8 » Tue Feb 19, 2013 9:55 pm

Yes, thanks guys, whilst searching around I ran across the threading and sched modules documentation. To a slap shot like me those look very intimidating, but, I guess, that is where I need to go!

Thanks again for the confirmation,
JP.
jogl8
 
Posts: 26
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: Scheduling Events

Postby micseydel » Tue Feb 19, 2013 11:39 pm

It looks like sched is the way to go, and it doesn't look to difficult to use. Do not use threading for this where sched can get it done instead.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Scheduling Events

Postby jogl8 » Wed Feb 20, 2013 1:53 pm

Yes sched works great!

Thanks,
JP.
jogl8
 
Posts: 26
Joined: Fri Feb 08, 2013 4:46 pm
Location: SW Ontario, Canada

Re: Scheduling Events

Postby verb » Fri Feb 22, 2013 8:25 pm

why don't you use cron ?
verb
 
Posts: 9
Joined: Fri Feb 22, 2013 8:15 pm

Re: Scheduling Events

Postby micseydel » Fri Feb 22, 2013 8:49 pm

cron is used for launching programs, not functions.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests