python中的线程程序在IDLE上成功运行,但在shell,命令行上没有



我在Windows 3.8上使用python 10。我能够在 IDLE 上成功运行线程程序,但是,相同的程序不会在命令行上启动,或者当我双击它们时。即使线程未启动,shell 也会弹出并快速退出。我什至试图捕获任何运行时错误和任何错误,除了但我在 IDLE 上都没有,程序仍然在 shell 上突然终止。

这是一个例子——

import threading
import time
try:
def func():
for i in range(10):
print(i)
time.sleep(0.1)                 
t1 = threading.Thread(target = func)
t1.start()
#t1.join() # i tried this also
while t1.is_alive():
time.sleep(0.1) #trying to return back, i added this when the threads were not working
input() #waiting for the user to press any key before exit
except RuntimeError:
print('runtime error')
input()
except: # any error
print('Some error')
input()

我发现我在目录中制作了一个名为"threading.py"的文件。此文件导致 python 的属性错误,因为它与 python 的"线程"模块具有相同的名称。我重命名了它,我的程序运行良好!

最新更新