How to launch a function at regular time intervals ?

MRAB python at mrabarnett.plus.com
Fri Aug 14 18:40:59 EDT 2009


David wrote:
> With your help, Franck, I think I finally got it to work. This is how
> I did it:
> 
> # In the main program, launch the 2 threads CStoreData and
> CTransferData, which will run indefinitely until they are stopped (if
> the threads were launched inside the while loop, there would be an
> infinitely growing number of threads CStoreData all doing the same
> thing, same for CTransferData)
> 
> func_store_data = CStoreData()
> func_store_data.start()
> 
> func_transfer_data = CTransferData()
> func_transfer_data.start()
> 
> # While loop, just to keep the threads running until they are stopped
> at the end of the program. If there wasn't a while loop, the threads
> would be stopped right after they are launched. The while loop
> separates the "start" of the threads from the "stop" of the threads
> 
> while 1
>   if end_condition:  # terminate program
>             break  # break out of while loop
> 
You might want to insert a sleep or something in there, otherwise you'll
be it'll consume a lot of CPU time doing nothing (a 'busy wait').

> # Stop all running threads at end
> 
> func_store_data.stop()
> func_store_data.join()
> 
> func_transfer_data.stop()
> func_transfer_data.join()
> 
> 
> Again thanks a lot to all of you for your precious help.
> 




More information about the Python-list mailing list