Python Forum
using threading.Timer for function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using threading.Timer for function
#1
Hello,
I have a simple function that I want it to run every 1 min
the function check internet connection - ping to google (8.8.8.8)

this is all the code
import os
import sys
import time
import datetime
import requests
import threading
import socket
import netifaces as ni

Error_Count = 0

def get_ip_address():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    return s.getsockname()[0]


def CheckConnection():
    #threading.Timer(60.0, CheckConnection).start() # called every minute
    global Error_Count
    address_check = '8.8.8.8'
    response = os.system('ping -c 2 ' + address_check)
    if response == 0:
        pingstatus = "Network Active"
        print(pingstatus, "count: ", str(Error_Count))
    else:
        if Error_Count == 3:
            #os.system('sudo shutdown -r now')
            print('put here reboot command if nothing works , need to check remove route maybe')
            Error_Count = 0
        else:
            errorTime = datetime.datetime.now()
            pingstatus = (str(errorTime) + " : Netwrok Down!")
            OutInterface = get_ip_address()
            print('trying to go out using : ' + OutInterface)
            time.sleep(2)
            boot_response = os.system('sudo sh /bin/modemstart > /home/pi/logs/modemstart.txt 2>&1 &')
            print(str(boot_response) + '\n\r Modem shoud be up , see IP')
            time.sleep(2)
            # ni.ifaddresses('wwan0')
            try:
                Wan_ip = ni.ifaddresses('wwan0')[ni.AF_INET][0]['addr']
            except ValueError as ve1:
                print(ve1)
            OpenVpnResponse = os.system('sudo openvpn /etc/openvpn/client.conf &')
            print(str(OpenVpnResponse) + '\n\r VPN should be UP , check it')
            try:
                Vpn_ip = ni.ifaddresses('tun0')[ni.AF_INET][0]['addr']
            except ValueError as ve:
                print(ve)
            Error_Count += 1
    return pingstatus

threading.Timer(60.0, CheckConnection).start() # called every minute
but when I run the code it only run me 1 time , and not every 60 seconds
what did I miss in my understanding of "threading.Timer"?

*** it there is a better\simple way to tp make it run once every x min - show me please ,

Thanks,


I have found and used this
https://schedule.readthedocs.io/en/stable/index.html
and it's seem to be working
Reply
#2
The docs for Timer say that the action (i.e. the function you pass) is called after the specified interval. They don't say that the action is called repeatedly at that interval.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,862 May-03-2023, 08:22 AM
Last Post: billykid999
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,130 Oct-01-2021, 08:32 PM
Last Post: muzikman
  email timer/rss feed timer ndiniz 1 2,088 Feb-02-2021, 07:18 PM
Last Post: nilamo
  Run a timer when a functions starts to see how long the function takes to complete Pedroski55 2 2,018 Apr-19-2020, 06:28 AM
Last Post: Pedroski55
  Help with Stopping a function after a timer SheeppOSU 0 1,939 Jan-28-2019, 10:13 PM
Last Post: SheeppOSU
  code keeps running if i use from threading import timer? birddseedd 3 2,608 Jan-25-2019, 05:00 AM
Last Post: stullis
  Cannot use function with timer/clock theangryprogrammer 1 3,323 Jan-22-2019, 04:22 PM
Last Post: Larz60+
  Is there a max value for threading.Timer function? whookie 1 3,979 Feb-03-2018, 09:51 PM
Last Post: ka06059
  Threading timer crashes my App panoss 4 6,517 Jun-05-2017, 05:57 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020