How do I run routines where timing is critical?

Hemanth P.S. hemanth_sethuram at yahoo.com
Mon Jun 21 08:44:40 CEST 2004


You can do something like this. I am assuming you can poll with 2
cameras every 0.3 seconds instead of 0.33333333... seconds.

# start of code 
class Camera():
    def __init__(self, clickInterval):
        self.clickInterval = clickInterval
    #
#

# List of camera objects with their periodicity
cameraList = [Camera(0.3), Camera(0.3), Camera(0.5), Camera(0.5), \
    Camera(0.5), Camera(0.5), Camera(1), Camera(1)]

globalTime = 0.0   # counter to keep track of current time
import time

while True:
    time.sleep(0.1)    # 0.1 seconds is the granularity
    globalTime += 0.1
    # examine each camera if it needs to capture at this moment
    for camera in cameraList:
        if globalTime % camera.clickInterval == 0.0:
            click(camera)  # capture image from this camera
        #
    #
#
# end of code

You have to add additional parameters to the Camera class to define
it's
address, etc. Every 0.1 second you determine which of the cameras to
be clicked and then capture images from those cameras.

--Hemanth P.S.

"S. David Rose" <photos at conversent.net> wrote in message news:<GorBc.75458$V57.10872803 at news4.srv.hcvlny.cv.net>...
> Hello All!
>    I am new to Python, and wanted to know if I might ask you a question
> regarding timing.  
> 
>   I want a main loop which takes photos from 8 different web-cams, each can
> be addressed by http://ip-addr/image.jpg.  That's the easy part, but I want
> to have 2 cameras poll 3-times a second, 4 cameras poll 2 times a second,
> and the remaining 2 cameras poll once a second.  I have found lots of info
> suggesting the use of threads for this, all using sleep to have a single
> event fire every second or so.  But, what I'd like to do is have a loop
> which does not need to 'sleep' but can rather evaluate where I am in the
> second and then have that logic trigger the event. 
> 
> In otherwords,
> 
> While 1
>   if second >1.00 and <1.25
>      Camera 1 & camera 2
>      Camera 5 & camera 6 & camera 7 & camera 8
> 
>   if second >1.25 and < 1.50
>      Camera 1 & camera 2
> 
>   if second >1.50 and <1.75
>      Camera 1 & camera 2
>      Camera 5 & camera 6 & camera 7 & camera 8
> 
>   if second >1.75 and < 2.0
>      Camera 1 & camera 2
> 
>   if second >1.00 and < 1.334
>      Camera 3 & camera 4
> 
>   if second > 1.334 and < 1.667
>      Camera 3 & camera 4
> 
>   if second > 1.667 and < 2.000
>      Camera 3 & camera 4
> 
> Please don't be too harsh with me.  I'm still new, and am still quite
> actively learning, but I've searched a bit on this and can't seem to find
> it on my own.
> 
> Thank you in advance!
> Dave Rose
> Stamford, CT - USA



More information about the Python-list mailing list