简体   繁体   English

如何在我的主代码旁边运行一个函数?

[英]How do I run a function alongside my main code?

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().我试过 .start()、.run() 和 .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.我在 while true 语句中运行所有代码。

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.这里greet是一个函数对象,而greet()调用函数。

In your case, you are calling the function, which is not intended.在您的情况下,您正在调用该函数,这不是故意的。

You have to pass the function object, threading.Thread(target=explain)您必须传递函数对象threading.Thread(target=explain)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的主 function 中同时运行 class? - How do I run a class concurrently in my main function? 如何使函数“ addlevels”在我的主代码中起作用? - How Do I make the function “addlevels” work in my main code? 我怎样才能在另一个功能旁边运行 websockets 服务器? - How could I run a websockets server alongside another function? 我必须在我的python代码中有main函数吗? - do I have to have main function in my python code? 如何在不出现 NameError 的情况下将不同目录中的函数导入到我的主代码中? - How do I Import a function from a different directory to my main code without getting a NameError? 如何通过main()函数中的pass命令使代码在Shell中保持打开状态? - How do I keep my code open in the shell with a pass command in the main() function? 如何在主脚本中运行另一个脚本 - How do I run another script, inside my main script 我如何修复 TypeError:当我运行它时,我的代码中 main.py 的第 4 行上预期有一个字符缓冲区 object - how do i fix TypeError: expected a character buffer object on line 4 in main.py in my code when i run it 如何在我的Swift软件应用程序中为我的函数计算运行Python代码? - How do I run Python code for my function calculations in my Swift software App? 如何调用 class 用于我的主要 function - How do I call a class for use in my main function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM