【发布时间】:2021-05-29 02:12:42
【问题描述】:
如果按下某个键,我想暂停并继续线程。 我试过:如果按下 q,它将删除(更改为 0)“time.sleep(99999)”,但它没有用任何人都可以帮助我吗?
import keyboard
from threading import Thread
from time import sleep
Thread1 = True
Thread2 = True
class main():
def test1():
if keyboard.is_pressed("q"): #if keyboard is pressed q it will reomve the sleep
time = 0
time = 99999
while Thread1 == True:
print("Thread1")
sleep(time)
def test2():
while Thread2 == True:
print("Thread2")
sleep(1)
Thread(target=test1).start()
Thread(target=test2).start()
main()
【问题讨论】:
-
顺便说一句,您的
main()是多余的。你的意思是def main():? -
这能回答你的问题吗? Pausing a thread using threading class
标签: python python-3.x multithreading keyboard python-multithreading