简体   繁体   English

如何使用其他脚本启动和停止脚本?

[英]How to start and stop scripts using another script?

I don't have a lot of experience coding so I'm sorry if this has been answered before; 我没有太多的编码经验,所以很抱歉以前没有回答过。 I couldn't find anything that helped. 我找不到任何有帮助的东西。

I just completed a project on a Raspberry Pi that runs some RGB LED strips via PWM. 我刚刚在Raspberry Pi上完成了一个项目,该项目通过PWM运行一些RGB LED灯条。 I have a program that runs the lights and works fine with a few different modes (rainbow shifting, strobe, solid color) but with each new mode I add the program get longer and more convoluted. 我有一个程序可以运行灯光,并且可以在几种不同的模式(彩虹平移,频闪,纯色)下正常工作,但是对于每种新模式,我添加的程序都会变得更长,更复杂。 I would like to have each separate mode be its own script that gets started or stopped by a sort of master script. 我希望每种单独的模式都可以是自己的脚本,可以通过某种主脚本来启动或停止。 That way I could easily add a new mode by simply writing a separate program and adding it to the list on the master script instead of mucking around inside a giant program with everything in it and hoping I don't break something. 这样,通过简单地编写一个单独的程序并将其添加到主脚本的列表中,我可以轻松地添加新模式,而不必在其中包含所有内容的巨型程序中乱搞,并希望我不会破坏某些内容。 I guess what I want is a simple way to start a python script with some specific setting (Determined by variables passed from the master script) and be able to kill that script when the master script receives the command to change modes. 我想我想要的是一种简单的方法来启动具有某些特定设置(由从主脚本传递的变量确定)的python脚本,并能够在主脚本收到更改模式的命令时终止该脚本。

Keeping your code modulable is indeed a good practice ! 保持代码可调制确实是一个好习惯! If your code is not Objet oriented, the best way is to create another python file (let's call it util.py) in the same directory as your "main". 如果您的代码不是面向Objet的,最好的方法是在与“主”文件相同的目录中创建另一个python文件(我们将其称为util.py)。 You can simply include util.py with the following command at the beginning of your main code : import util And then when you want to use a function that you've defined in your util.py file, juste use : util.myFunction(param1, param2,...) 您可以在主代码的开头简单地将util.py包含在以下命令中:import util然后,当您要使用在util.py文件中定义的函数时,只需使用:util.myFunction(param1 ,param2,...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM