Home>
def get_data ():
def countdown ():
  time.sleep (5)
  print ('bed')
  continue
def main ():
    check= Timer (30.0, countdown)
    pars= Thread (target= get_data)
    check.start ()
    pars.start ()
    check.cansle ()
for accaunt in bd:
   name= accaunt.split (':') [0]
   pas= accaunt.split (':') [1]
   user= wa (name)
   main ()

How can this be done, task:get_datamay freeze, but if it works, it will have time to execute in less than 5 seconds. According to my logiccountdownstarts earlierget_dataand sleeps for 5 seconds. Ifget_dataworked,countdownwill be canceled, and if it hangs,countdownshould causecontinuefor the loop and force it to move on to checking the next item. How real is it, and how to cause thiscontinue?

  • Answer # 1

    GrAnd thanks, your version works quite well for me. And this is what I managed to find, but there are a few extra signals sends, now we will see who will show himself better)

    ** from wrapt_timeout_decorator import ***
        def get_data ():
        ** @ timeout (5) **
        def main ():
            get_data ()
        for accaunt in bd:
           name= accaunt.split (':') [0]
           pas= accaunt.split (':') [1]
           user= wa (name)
           main ()
    
  • Answer # 2

    You can try like this:

    import multiprocessing
    import time
    def get_data (x, n):
        for i in range (1, n + 1):
            print (f "{x}: Get Data {i} of {n}")
            time.sleep (1)
    def main (x, n):
        p= multiprocessing.Process (target= get_data, args= (x, n))
        p.start ()
        p.join (5) # wait no more than 5 seconds
        if p.is_alive (): # if the thread is still running
            print (f "[{x}: {n}] Timeout !!! Kill 'em all: -E")
            p.terminate ()
            p.join ()
    for account, n in [("my", 10), ("your", 3), ("their", 1)]:
       main (account, n)
    
    my: Get Data 1 of 10
    my: Get Data 2 of 10
    my: Get Data 3 of 10
    my: Get Data 4 of 10
    my: Get Data 5 of 10
    [my: 10] Timeout !!! Kill 'em all: -E
    your: Get Data 1 of 3
    your: Get Data 2 of 3
    your: Get Data 3 of 3
    their: Get Data 1 of 1