threading.Thread 没有在函数上执行循环
threading.Thread not executing loops on functions
我一直在尝试编写一个自动点击器,但我首先需要学习如何使用 Thread 库,但我不知道为什么代码在循环第一次运行时就停止了.
import time
from threading import Thread
def countdown(n):
while n > 0:
print('T-minus', n)
n -= 1
time.sleep(5)
t = Thread(target = countdown, args =(10, ))
t.start()
输出只有:
>>> T-minus 10
谁能帮帮我?
我才明白是怎么回事!如果您尝试 运行 Jupyter Cell 上的线程,它不会显示输出,我稍微更改了代码以对其进行测试。
import time
from threading import Thread
TIMES = 0
def countdown(n):
global TIMES
while n > 0:
TIMES += 1
time.sleep(5)
t = Thread(target = countdown, args =(10, ))
t.start()
在下一个单元格中,我一直在打印 TIMES 值,它正在发生变化!但只是在后台!
for i in range(10):
time.sleep(3)
print(TIMES)
我一直在尝试编写一个自动点击器,但我首先需要学习如何使用 Thread 库,但我不知道为什么代码在循环第一次运行时就停止了.
import time
from threading import Thread
def countdown(n):
while n > 0:
print('T-minus', n)
n -= 1
time.sleep(5)
t = Thread(target = countdown, args =(10, ))
t.start()
输出只有:
>>> T-minus 10
谁能帮帮我?
我才明白是怎么回事!如果您尝试 运行 Jupyter Cell 上的线程,它不会显示输出,我稍微更改了代码以对其进行测试。
import time
from threading import Thread
TIMES = 0
def countdown(n):
global TIMES
while n > 0:
TIMES += 1
time.sleep(5)
t = Thread(target = countdown, args =(10, ))
t.start()
在下一个单元格中,我一直在打印 TIMES 值,它正在发生变化!但只是在后台!
for i in range(10):
time.sleep(3)
print(TIMES)