'Python Twitter Bot Main Files not running all functions

I created an automatic Twitter bot with a group of friends during my university's Hackathon. As a new Python developer, I came across an issue where certain functions of my driver code were not being reached and never running. That's when I remembered that Python has an interpreter compiler.

We created a folder containing all of our bot's functions and then called all said functions in our main file. Our main file's code looks something like this:

from bot_functions.get_quote import quote_on_enable
from bot_functions.reply import reply_on_enable
from bot_functions.retweet import retweet_on_enable

while True:
    quote_on_enable() # Always runs
    reply_on_enable() # Sometimes runs
    retweet_on_enable() # Code cannot be reached.

Examples of some of our function's code (functions are all in seperate files, but share the same folder:

def quote_on_enable(): # Purpose to run code infinitely.
    while True:
        try:
            quote = get_quote() # Uses helper function above.
            tweet = "..." + "\n\n" + "Komi-Translation: " + "\n" + quote
            print('\n Tweeting: ' + '\n' + tweet)
            api.update_status(tweet) # Actually tweeting
            print("Next quote in 10 seconds.")
            time.sleep(10) # Halts the loop for 'x' amount of time
        except Exception as error:
            print(error)
            break

def reply_on_enable():
    bot_id = int(api.verify_credentials().id_str)
    mention_id = 1

    message = "@{} ..."

    while True:
        mentions = api.mentions_timeline(since_id=mention_id) # Finding mention tweets
        for mention in mentions:
            print("Mention tweet found")
            print(f"{mention.author.screen_name} - {mention.text}")
            mention_id = mention.id
            # Checking if the mention tweet is not a reply, we are not the author, and
            # that the mention tweet contains one of the words in our 'words' list
            # so that we can determine if the tweet might be a question.
            if mention.in_reply_to_status_id is None and mention.author.id != bot_id:
                    try:
                        print("Attempting to reply...")
                        api.update_status(message.format(mention.author.screen_name), in_reply_to_status_id
id_str, auto_populate_reply_metadata = True)
                        print("Successfully replied :)")
                    except Exception as exc:
                        print(exc)
        time.sleep(15) # The bot will only check for mentions every 15 seconds, unless you tweak this value

I believe that since all of our functions are calling while True, we will never be able to leave the function calls. How should I go about solving this issue? Is it possible for me to maintain the format I have going right now, or will I have to end up throwing all the functions into the main file? Thank you!I



Solution 1:[1]

By removing all my functions While True: loops as well as adding exceptions to all the proper locations, I have fixed this issue.

Solution 2:[2]

The problem is not related to the file where your functions are.

You are first calling the quote_on_enable function and, because of the while True: loop inside, you stay inside until an exception is caught (because, in that case, you are calling a break).

So you call your second function, reply_on_enable, only when it happens. And once you are there, you are stuck inside because there is no way out its infinite while True: loop.

And this is why your third function is never reached.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Danny Chen
Solution 2 Mickael Martinez