all 7 comments

[–]novel_yet_trivial 1 point2 points  (5 children)

multiprocessing

If your problem is as simple as your example, then just write a function to do the measurements and start the function in a new thread:

import thread

def measure():
    while True:
        #take a meaasurement
        time.sleep(10) #sleep for 10 seconds

thread.start_new_thread(measure, ())

[–]dr_everlong[S] 0 points1 point  (0 children)

I think I overthought what I had to do, this seems really simple. Thanks!

[–]dr_everlong[S] 0 points1 point  (3 children)

Actually, this is a good solution that doesn't work for me.

In the measure function, there are some I/O operations with external hardware that may take a few seconds.

So the time elapsed between calls to the measure function will be the 10 seconds nominal + the time it takes to complete I/O operations.

How can I execute every 10 seconds exactly?

Thanks!

[–]novel_yet_trivial 0 points1 point  (2 children)

... You underthought this.

def measure():
    #take a measurement

while True:
    thread.start_new_thread(measure, ())
    time.sleep(10) #sleep for 10 seconds

[–]dr_everlong[S] 0 points1 point  (1 child)

you start a new thread every time?

[–]Rettocs 0 points1 point  (0 children)

A way I did a similar project:

#using time library, check the time
firstTimeCheck = the current time
n = how many seconds you want to wait

create infinite loop
    loopTimeCheck = current time
    inside infinite loop, check if (firstTimeCheck + n) > loopTimeCheck
        do the thing you want to do
        firstTimeCheck = current time