Yaharo.
If you write subprocess.run ('cat path', shell = True)
in python, repent with me and use ʻopen ()` from tomorrow.
subprocess
?If you are familiar with it, please skip it.
subprocess
is a module that executes Linux OS commands from python.
There are os function and system function to execute OS commands on Python, but it seems that subprocess
is currently recommended.
import subprocess
list = subprocess.run('ls')
#hoge.py hoge.sh hoge.wav
print(list)
#CompletedProcess(args='ls', returncode=0)
#If you pass a command as an argument, the execution result (status) will be returned.
list = subprocess.run('ls', encoding='utf-8', stdout=subprocess.PIPE).stdout
print(list)
#hoge.py
#hoge.sh
#hoge.wav
#By specifying encoding, utf-Can be encoded with 8 etc.
#You can pass output to standard output by specifying the stdout attribute
list = subprocess.run('ls -a', shell=True, encoding='utf-8', stdout=subprocess.PIPE).stdout
print(list)
#.hoge
#hoge.py
#hoge.sh
#hoge.wav
#shell=You can include spaces by specifying True, but there is a risk of command injection etc.
You can execute various OS commands like this, but the process is slow because a subprocess is created each time. The image is that parents bother to give birth to a child to read a book and have the child read aloud. If you read it yourself, you can solve it, but it's understandable that it's heavy.
By the way, read aloud the option of stdout = subprocess.PIPE
! It means.
Parents cannot know what the child is reading (unless they can read their thoughts with the sixth sense), but they can do so by reading aloud.
Although it is dissed in this article, it is a very convenient module, so if you are new to it, please use it. ~~ Processing is not so heavy for normal use. ~~ (Plan collapse)
cat
was stupid in subprocess
As mentioned above, you can get the text content of hogehoge.txt by doing subprocess.run ('cat /hogehoge.txt', shell = True)
, but you can start a subprocess just to pull the text. Since it is raised, the processing will be slow.
You can use a built-in function called ʻopen ()` to open a file in Python, so let's use that.
It's very rudimentary, but I didn't know how to use it easily.
with open('/hogehoge.txt') as f:
hoge = f.read()
print(hoge)
#/hogehoge.The text of txt is displayed
Since anything opened with ʻopen ()must be closed with
close (), we use
with to avoid forgetting to close it. After closing ʻopen ()
, the file can no longer be referenced, but the assigned variable can be referenced.
See the official documentation (https://docs.python.org/ja/3/library/functions.html#open) for more information.
I wrote the following script and verified it.
#!/usr/bin/python3.7
import subprocess
import time
#open()
start = time.time() #Get start time
for i in range(1000): #Repeat 1000 times
with open('/sys/class/thermal/thermal_zone0/temp') as f: #Open file
test = f.read() #Substitute the text of the file
elapsed_time = time.time() - start #Get the end time and find the difference from the start time
print("Open: {}s".format(elapsed_time)) #Write out elapsed time
#subprocess
start = time.time()
for i in range(1000):
test = subprocess.run('cat /sys/class/thermal/thermal_zone0/temp', shell=True, stdout=subprocess.PIPE).stdout
elapsed_time = time.time() - start
print("Subprocess: {}s".format(elapsed_time))
#vcgencmd in subprocess (reference record for CPU temperature)
start = time.time()
for i in range(1000):
test = subprocess.run('vcgencmd measure_temp', shell=True, stdout=subprocess.PIPE).stdout
elapsed_time = time.time() - start
print("Vcgencmd: {}s".format(elapsed_time))
The execution result is as follows.
Open: 0.08225083351135254s
Subprocess: 5.188024044036865s
Vcgencmd: 6.032892465591431s
What do you mean! Subprocess, which was so slow, is now more than 50 times faster.
I think that the breakdown of the elapsed time is probably the standby time, so I can not say simply about the CPU load, but it is certain that it is light as if I was monitoring from another window. If you execute it several times, there will be some variation, but if it is so different, you will want to use ʻopen ()`.
By the way, in subprocess.run
, the processing of the parent process is stopped until the child process ends, but by using subprocess.Popen
, the child process can be turned to the background and parallel processing can be performed.
Even if you use subprocess.Popen
, it took about 2s in the above verification, so there is no doubt that ʻopen ()` is fast, but the difference may be reduced depending on the situation.
In any case, the purpose of this time is to get a text file, so I want to avoid parallelism ...
This verification is triggered by the improvement of the script dealt with in the article Measuring the CPU temperature of Raspberry Pi with Python, so check it with vcgencmd
as a reference. I tried it.
The output of vcgencmd
is formatted so that the temperature value is more readable, so it is likely to be slower.
[Addition] Based on the contents of this article, I wrote Script to monitor CPU with Python.
I was surprised to see a clearer difference than I expected. I want to study more and more and be able to write programs that are conscious of "the right person in the right place".
Recommended Posts