简体   繁体   English

从一个文件运行多个Python脚本

[英]Running multiple Python Scripts from one file

I have 3 scripts that needs to be executed. 我有3个需要执行的脚本。 Two of them have continuous loop and will never stop runing as long as sensors are sending the data, and 3rd script will run only once every hour. 它们中的两个具有连续循环,只要传感器发送数据就不会停止运行,并且第3个脚本每小时仅运行一次。

So lets say i have them like this: 所以可以说我有这样的他们:

sensorscript1 sensorscript2 Export sensorscript1 sensorscript2导出

What would be the best method to create one single file to run this process? 创建一个文件来运行此过程的最佳方法是什么? Would using threading be the best way to go in this case? 在这种情况下,使用线程将是最好的方法吗?

import sensorscript1, sensorscript2
from threading import Thread

Or would Flask app be better suited for this? 还是Flask应用程序更适合于此? Any other suggestions? 还有其他建议吗?

There is another approach to do this if you want each script to run in a new window & maybe look at the logs on it. 如果您希望每个脚本在新窗口中运行并查看其日志,则还有另一种方法

You could run one script that calls the other scripts using a subprocess call. 您可以运行一个使用subprocess调用来调用其他脚本的脚本。

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)

Or alternatively you can run Export & call the other two scripts from it - it's upto you to decide how it runs best for you. 或者,您也可以运行Export并从中调用其他两个脚本-由您自行决定如何最好地运行它。

Use PyThreads, a new threading library for python. 使用PyThreads,这是python的新线程库。 https://github.com/Narasimha1997/PyThreads , this makes running threads much more easier. https://github.com/Narasimha1997/PyThreads ,这使运行线程更加容易。

For example you have 3 script files, f1, f2 and f3, write functions in all these 3 files as threads using PyThreads 例如,您有3个脚本文件f1,f2和f3,使用PyThreads将所有这3个文件中的函数写为线程

Example : File -1 示例:文件-1

from pythreads import pythread
@pythread
def fun1() : 
  #some logic
  pass

File -2: 文件-2:

from pythreads import pythread

@pythread
def fun2():
   #some logic
   pass

Now in main file import them 现在在主文件中导入它们

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: I wrote PyThreads to make thread usage easier in python PS:我编写了PyThreads来简化python中的线程使用

You pretty much got it 你差不多了

import sensorscript1, sensorscript2
from threading import Thread

t1 = Thread(target=sensorscript1, args=(arg1, arg2))
t2 = Thread(target=sensorscript2, args=(arg,))

t1.run()
t2.run()

Note the args parameter is only needed if you are passing arguments to the function. 请注意,仅在将参数传递给函数时才需要args参数。 Also note the extra comma. 另请注意多余的逗号。 This is because the args parameter requires a Tuple 这是因为args参数需要一个元组

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

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