【发布时间】:2021-01-27 06:03:18
【问题描述】:
我正在尝试从单个 python 代码运行三个 python 代码。但是,代码不会从终端运行。但是,当我尝试使用 Thonny IDE 运行它时,所有代码都运行良好。下面是我要执行的代码:
#!/usr/bin/python3
import time
import threading
import os
def startProgram(i):
if i==0 or i=='0':
time.sleep(1)
os.system("sudo python3 /home/pi/a.py")
print("Create tunnel code executed")
elif i==1 or i=='1':
time.sleep(1)
os.system("sudo python3 /home/pi/b.py")
print("Create capture http code executed")
elif i==2 or i=='2':
time.sleep(1)
os.system("sudo python3 /home/pi/c.py")
print("Create mail transaction details code executed")
else:
print("Value not in range")
pass
def main():
for i in range(3):
print(i)
t = threading.Thread(target = startProgram, daemon=True, args=(i,))
t.start()
if __name__=="__main__":
main()
终端没有抛出错误。代码只是运行并退出,但其他 3 个程序永远不会执行。我正在尝试使用以下命令在终端中执行 python 代码:
python3 startProgram.py
另外,我已经单独测试了所有代码,它们运行时没有任何错误。请让我知道我哪里出错了以及如何解决这个问题。提前致谢。
【问题讨论】:
-
尝试从每个 os.system 调用中删除“sudo”。
-
尝试删除“sudo”。仍然没有从终端执行。
-
如果您的主线程结束并且只剩下守护线程,程序将不正常地关闭。你应该加入
main()末尾的线程。
标签: python python-3.x raspberry-pi python-multithreading