-2
def explain():
    Timer = 60
    keyboard.press("/")
    time.sleep(0.1)
    keyboard.write(StillInBeta)
    time.sleep(0.8)
    keyboard.press('enter')
    time.sleep(0.8)
    keyboard.press("/")
    time.sleep(0.1)
    keyboard.write(Reminder)
    time.sleep(0.8)
    keyboard.press('enter')
    time.sleep(0.1)
    for x in range(60):
        Timer = Timer - 1
        time.sleep(1)
        print(Timer)
Explain_And_Remind = threading.Thread(target=explain())
Explain_And_Remind.join()

I've tried .start(), .run(), and .join(). Searching gave me nothing. I'm not sure how to go about doing this. I'm running all code in a while true statement.

  • It's not very clear what you want to do here. However, if I'm understanding correctly, you want to run a function separately outside of your while loop? Then you can just start a new process.. I.e. put your other function in another script and run that separately? You'll need to provide more details if you want better answers – Ashish Oct 28 '21 at 04:37
  • @Ashish Best details of what I want that I can provide; I need to have a separate function running alongside a main function, both inside a while loop. Every 60 seconds it'll print something as you can see, while waiting every 60 seconds for it to print, I need my main script to read a screenshot. Not sure how else to put it. – Kreighstan Oct 28 '21 at 04:39
  • 1
    `target=explain()` means "call `explain` *now*, and use *the result* as the target". The `()` are **not** a part of the name of the function; they are how you call it. You want the `target` to be *the function*: `target=explain`. This is a common typo for those who understand it, and a common misunderstanding - but one that should be covered by any proper tutorial - for those who don't. – Karl Knechtel Oct 28 '21 at 04:39
  • "Searching gave me nothing" I literally tried putting `python run function alongside my code` [into a search engine](https://duckduckgo.com/?q=python+run+function+alongside+my+code) and I see somewhat useful results. It gets much better if I try adding words like `threading` to the search terms. Exactly what did you try searching for, and exactly what did you see? – Karl Knechtel Oct 28 '21 at 04:41
  • @Karl Knechtel I'll try what you said, when I searched it, what I searched we're along the lines of, "how to run thread with main code" or, "how to run thread alongside something", they all gave me the same thing. So I tested it and it ran alongside my main function, but it stopped my main function from running multiple times. – Kreighstan Oct 28 '21 at 04:43
  • Alternately, if I try `python thread runs immediately` - i.e. the *problem you are experiencing*, but didn't actually try to explain - I find [this previous question](https://stackoverflow.com/questions/11792629/thread-starts-running-before-calling-thread-start). – Karl Knechtel Oct 28 '21 at 04:43
  • " I'll try what you said, when I searched it, what I searched we're along the lines of, "how to run thread with main code" or, "how to run thread alongside something", they all gave me the same thing. " I just copied and pasted `how to run thread with main code` into the same search engine and the first thing I see is a [Youtube video tutorial about how to do it in Python](https://www.youtube.com/watch?v=IEEhzQoKtQU), even though it doesn't even say Python in the query. On the sidebar, I get [this Stack Overflow question](https://stackoverflow.com/questions/11123621/). Etc. – Karl Knechtel Oct 28 '21 at 04:46
  • That's DuckDuckGo, so it shouldn't be personalizing search requests; but I tried again in incognito mode just to make sure. I struggle to believe that you actually tried the searches you claim to have tried. Everything I can think of trying works fine. – Karl Knechtel Oct 28 '21 at 04:47
  • @Karl Knechtel Incase you didn't see my edit, So I removed the parenthesis and it ran alongside my main function, but it stopped my main function from running multiple times, which is what I need it to do. – Kreighstan Oct 28 '21 at 04:51
  • Stack Overflow is not showing me any edit, even when I refresh the page. Regardless, you should probably try to ask a new question, making sure that you have a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and a clear description of both expected and actual behaviour. – Karl Knechtel Oct 28 '21 at 04:54
  • @Karl Knechtel Yea I should probably start doing that. – Kreighstan Oct 28 '21 at 04:56
  • @Karl Knechtel I figured out I couldn't loop the 2 in the same loop, thanks for the answer to my problem. – Kreighstan Oct 28 '21 at 04:58

1 Answers1

0

This is a very common mistake, even experienced people also miss these paranthesis.

The target argument of the thread takes a function object. For example -

def greet():
    print('Hello!')

Here greet is a function object, whereas greet() calls the function.

In your case, you are calling the function, which is not intended.

You have to pass the function object, threading.Thread(target=explain)