As I do in python for at the end of the code, it go to the start again

Asked

Viewed 92 times

-1

As I do in python to at the end of the code, it go to the start again and repeat once every 3600 seconds?

My code: *

fb     = Facebook ( "meu-token" )
fb.testing = False

try:
    start_time = time.time()
    markov.collecttext("./markov")
    #markov.collectjsons ("./markov")
    #markov.loadjson("data.json")
    #markov.savejson()
    train_time = time.time() - start_time

    start_time = time.time()
    generated = markov.generate (1)
    generation_time = time.time() - start_time

    if getpyversion() == 2:
        generated = generated.encode('utf8')

    image = randomimg("./images", generated)
    log("Chosen image: " + str(image))

    if not image:
        image = randomimg("./images")
        log("No tags found, randomly chosen image: " + str(image))
        message_start = "Este post foi gerado pelo Marcos Bot\n----------\n"
    else:
        log("tags: " + ", ".join(gettags(generated, image)))
        message_start = "The image was chosen because of the following tag(s): " + ", ".join(gettags(generated, image)) + "\n----------\n"

    response = fb.publish_image ( censor(generated), image )

    log(response)
except Exception as e:
    log("Failed to generate sentence. Posting a random image instead.")
    log(traceback.format_exc(e))
    message = "Failed to generate sent(i)ence. Posting a random image instead:"

    image = randomimg("./images")
    log("Chosen image: " + str(image))

    if not image:
        response = fb.publish_text ( message )
    else:
        response = fb.publish_image ( message, image )

    log(response)

main()*

3 answers

0

import threading
def my_function():
  threading.Timer(3600, my_function).start()
  print("Hello, World!")

my_function()

See working on repl it., I put 5 seconds for you to see how it works.

0

creates a loop "while true:" so the program will repeat itself endlessly, and just implement a flag at the end to exit the loop and terminate the program.

-2

Another option (not only in python would turn this into a recursive function, which calls itself at the end of the run...

  • 2

    This would be highly costly, virtually unfeasible. :-(

  • Yes it may be, but it’s still an option =]

  • 1

    An impossible option. :-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.