Home > Mobile >  How to make functions auto run every 5 minutes?
How to make functions auto run every 5 minutes?

Time:05-26

I wrote proxy checker and I need to automate checking for every 5 minutes. My while True loop skips the functions. When I have first iteration - functions are running, but with next iterations they are skipped, just printing START!!!!!! and END---- in my console.

if __name__ == "__main__":
    while True:
        print("START!!!!!!!!!!")
        http_proxy_check()
        save_proxy('http')
        print("END------------------")
        time.sleep(2)

http_proxy_check function:

def http_proxy_check():
    for proxy in http_proxyList:
        print(f"Checking {http_proxyList.index(proxy)} of {len(http_proxyList)} element")
        thread = Thread(target=checkIp_http,args=(proxy.strip(), ))
        if len(threads)<=1200 :
            thread.start()
            threads.append(thread)
        else:
            threads.clear()

    for thread in threads:
        thread.join()

CodePudding user response:

You are directly trying to run subthreads. Why are you not using multiprocess?

Also you can try to "timeloop" library for periodic tasks; https://pypi.org/project/timeloop/

pip install timeloop
import time

from timeloop import Timeloop
from datetime import timedelta

tl = Timeloop()

@tl.job(interval=timedelta(minutes=5))
def sample_job_every_5m():
    print "5m job current time : {}".format(time.ctime())

CodePudding user response:

while True:
    your_code_that_process()
    time.sleep(5*60)  # 300 seconds
    if check_for_abort_if_so_break():
        break
  • Related