Re: How to launch a function at regular time intervals ?



On 13 août, 21:28, Dave Angel <da...@xxxxxxxx> 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.host = ""
self.port = 21
self.timeout = 60
self.user = ""
self.passwd = ""

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

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

def run(self):
while not self.event.is_set():
file_handler = open("Test.txt", 'a')
file_handler.write(self.timestamp.strftime("%y%m%d%H%M%S
\n"))
file_handler.close()
self.event.wait(10)

def stop(self):
self.event.set()

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

def __init__(self, ftp_connection):
threading.Thread.__init__(self)
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('')
Ftp_handler.connect(self.ftp_connection.host,
self.ftp_connection.port, self.ftp_connection.timeout)
Ftp_handler.login(self.ftp_connection.user,
self.ftp_connection.passwd)
Ftp_handler.storbinary("STOR Test.txt", file_handler)
file_handler.close()
Ftp_handler.close()
self.event.wait(20)

def stop(self):
self.event.set()

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

while(1):
timestamp = datetime.now()
func_store_data = CStoreData(timestamp)
func_store_data.start()

func_transfer_data = CTransferData(ftp_connection)
func_transfer_data.start()

func_store_data.stop()
func_store_data.join()

func_transfer_data.stop()
func_transfer_data.join()
.



Relevant Pages

  • Re: Python and timezones
    ... from datetime import datetime ... def getZoneNamesOnThisComputer: ...     eg: ... # Why is America/Iqaluit showing up here??? ...
    (comp.lang.python)
  • Re: How to derive from DateTime?
    ...   def initialize ... Specializing other classes works fine but DateTime fails...I see, ...
    (comp.lang.ruby)
  • Re: Calendar Stuff
    ... import calendar, datetime ... def cal: ...   myCal = calendar.Calendar ...
    (comp.lang.python)
  • Re: Self function
    ... Python ... giving the programmer the ability to have a function refer to ... def parrot: ...     print inspect.getmembers ...
    (comp.lang.python)
  • Re: What c.l.pys opinions about Soft Exception?
    ... Actually, the latter is even less cluttered, misses a raise - if pure number ... Exception that aren't handled when no handler exists for it. ...     raise_soft ... def a_equal_b: ...
    (comp.lang.python)

Loading