How to launch a function at regular time intervals ?

David davigier at
Fri Aug 14 00:52:52 CEST 2009

On 13 août, 21:28, Dave Angel <da... at> wrote:
> David wrote:
> > Thanks all for your answers. As suggested by Dave and Frank, I am
> > indeed looking for the main program to continue running in the
> > background (I have several functions I want to launch, each at a
> > predefined time interval). I like Frank's solution, on the paper it
> > seems it would do what I am looking for, but I cannot succeed in
> > having it working. I guess I've been stuck with this problem for too
> > long and can't succeed in using my brain accurately anymore... ;-)
> > I defined the class as defined by Frank, and I then inserted the
> > following code in a While True loop, without any other code (the idea
> > is just to test Frank's solution before really using it in my
> > program):
> >     func = MyFunction()
> >     func.start()
> >     func.stop()
> >     func.join()
> > However I'm not getting the expected behavior. It's not taking into
> > account the 30 sec wait, the function is called again and again
> > without any time interval... Any idea ?
> > Again, thanks a lot.
> Why don't you include the code you're actually trying, instead of just
> trying to describe it.  Frank's class didn't call any function, it just
> had a place to do it.  So we really don't know what you're running, nor
> what about it is wrong.
> Perhaps a few well placed print statements?
> DaveA

Yes, I guess it would be more simple. Here is really what I am trying
to do. I simplified the functions, but the purpose is to write some
text in a local file every x seconds (here, I'm just writing the
timestamp, i.e. a string representing the date & time, every 10
seconds) and to transfer this file to a distant server via FTP every y
seconds (20 seconds in the example below). My code is a little bit
more complicated because each time I transfer the file, I delete the
local file which is then recreated when data is written, but for
simplicity I left this out in the code below. So, here is the code
I've been using to test Frank's code. I've been struggling with using
or not a While True loop or not, and everything I try seems to run
into issues.

import threading
from datetime import datetime
import ftplib

class CFtpConnection:
    """FTP Connection parameters"""
    def __init__(self, host, port, timeout, user, passwd): = ""
        self.port = 21
        self.timeout = 60
        self.user = ""
        self.passwd = ""

class CStoreData(threading.Thread):
    """Write timestamp in a file every 10 seconds in separate

    def __init__(self, timestamp):
        self.event = threading.Event()
        self.timestamp = timestamp

    def run(self):
        while not self.event.is_set():
            file_handler = open("Test.txt", 'a')

    def stop(self):

class CTransferData(threading.Thread):
    """Transfer timestamp file every 20 seconds in separate thread"""

    def __init__(self, ftp_connection):
        self.event = threading.Event()
        self.ftp_connection = ftp_connection

    def run(self):
        while not self.event.is_set():
            file_handler = open("Test.txt", 'r')
            Ftp_handler = ftplib.FTP('')
self.ftp_connection.port, self.ftp_connection.timeout)
            Ftp_handler.storbinary("STOR Test.txt", file_handler)

    def stop(self):

ftp_connection = CFtpConnection("", 21, 60, "", "") = ''
ftp_connection.user = "admin"
ftp_connection.passwd = "admin"

  timestamp =
  func_store_data = CStoreData(timestamp)

  func_transfer_data = CTransferData(ftp_connection)



More information about the Python-list mailing list