从一个文件运行多个Python脚本
我有3个脚本需要执行。其中两个具有连续循环,只要传感器发送数据,就不会停止运行,第三个脚本每小时仅运行一次 让我们这样说吧: 传感器脚本1 传感器脚本2 出口 创建单个文件以运行此过程的最佳方法是什么? 在这种情况下,使用线程是最好的方法吗从一个文件运行多个Python脚本,python,python-3.x,flask,Python,Python 3.x,Flask,我有3个脚本需要执行。其中两个具有连续循环,只要传感器发送数据,就不会停止运行,第三个脚本每小时仅运行一次 让我们这样说吧: 传感器脚本1 传感器脚本2 出口 创建单个文件以运行此过程的最佳方法是什么? 在这种情况下,使用线程是最好的方法吗 import sensorscript1, sensorscript2 from threading import Thread 还是Flask应用程序更适合这种情况? 还有其他建议吗?使用PyThreads,这是一个新的python线程库,这使得运行线程
import sensorscript1, sensorscript2
from threading import Thread
还是Flask应用程序更适合这种情况?
还有其他建议吗?使用PyThreads,这是一个新的python线程库,这使得运行线程更加容易 例如,您有3个脚本文件f1、f2和f3,使用PyThreads将所有这3个文件中的函数作为线程写入 例如: 文件-1
from pythreads import pythread
@pythread
def fun1() :
#some logic
pass
文件-2:
from pythreads import pythread
@pythread
def fun2():
#some logic
pass
现在在主文件中导入它们
from file1 import fun1
from file2 import fun2
#call these functions, because of @pythreads, they start behaving like threads
fun1()
fun2()
#your function 3
def fun3():
#some logic
pass
#call it here
if __name__ == "__main__" :
fun3()
PS:我编写PyThreads是为了使python中的线程使用更容易使用PyThreads,一个新的python线程库,这使得运行线程更加容易 例如,您有3个脚本文件f1、f2和f3,使用PyThreads将所有这3个文件中的函数作为线程写入 例如: 文件-1
from pythreads import pythread
@pythread
def fun1() :
#some logic
pass
文件-2:
from pythreads import pythread
@pythread
def fun2():
#some logic
pass
现在在主文件中导入它们
from file1 import fun1
from file2 import fun2
#call these functions, because of @pythreads, they start behaving like threads
fun1()
fun2()
#your function 3
def fun3():
#some logic
pass
#call it here
if __name__ == "__main__" :
fun3()
PS:我编写PyThreads是为了使python中的线程使用更容易,如果您希望每个脚本在新窗口中运行,可以查看其中的日志,那么有另一种方法来执行此操作。 您可以使用
子流程
调用运行一个调用其他脚本的脚本
import subprocess
subprocess.call("start cmd /K python sensorscript1.py", shell=True)
# this opens the script1.py file in a new console window (shell=True)
subprocess.call("start cmd /K python sensorscript2.py", shell=True)
subprocess.call("start cmd /K python Export.py", shell=True)
或者,您也可以运行Export并从中调用其他两个脚本-这取决于您决定如何运行它最适合您。如果您希望每个脚本在新窗口中运行,可以使用另一种方法执行此操作,或者查看其中的日志。 您可以使用
子流程
调用运行一个调用其他脚本的脚本
import subprocess
subprocess.call("start cmd /K python sensorscript1.py", shell=True)
# this opens the script1.py file in a new console window (shell=True)
subprocess.call("start cmd /K python sensorscript2.py", shell=True)
subprocess.call("start cmd /K python Export.py", shell=True)
或者,您也可以运行Export并从中调用其他两个脚本-这取决于您决定如何运行它最适合您。您基本上明白了
import sensorscript1, sensorscript2
from threading import Thread
t1 = Thread(target=sensorscript1, args=(arg1, arg2))
t2 = Thread(target=sensorscript2, args=(arg,))
t1.run()
t2.run()
注意:只有将参数传递给函数时,才需要args参数。还要注意额外的逗号。这是因为args参数需要一个元组您基本上得到了它
import sensorscript1, sensorscript2
from threading import Thread
t1 = Thread(target=sensorscript1, args=(arg1, arg2))
t2 = Thread(target=sensorscript2, args=(arg,))
t1.run()
t2.run()
注意:只有将参数传递给函数时,才需要args参数。还要注意额外的逗号。这是因为args参数需要一个元组谢谢您的快速回答。但是,由于PyThreads尚未发布,我如何安装它呢?
fun1()
和fun2()
仍应在main
中调用。只需将其添加到项目文件夹中,是的,您可以从main alsoDisclaimer调用它:这是一个库,他编写了感谢您的快速回答。但是,由于PyThreads尚未发布,我如何安装它呢?fun1()
和fun2()
仍应在main
中调用。只需将其添加到项目文件夹中,是的,您可以从main alsoDisclaimer调用它:这是他编写的库。我认为运行您的解决方案ParvBanks可能更容易。我要试试。太好了!一定要让我知道进展如何。。。我自己在一个项目中使用过它,所以我知道它是有效的。优点是您可以分别监视每个窗口中的日志。我认为运行您的解决方案ParvBank可能更容易。我要试试。太好了!一定要让我知道进展如何。。。我自己在一个项目中使用过它,所以我知道它是有效的。其优点是,您可以分别监视每个窗口中的日志。