In How to pass the execution result of shell command in Python as a list, there was such a source. This is a good idea, but it's a shame to wait for the shell command to finish.
res_cmd_no_lfeed.py
#!/usr/bin/python
import subprocess
def res_cmd_lfeed(cmd):
return subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.readlines()
def res_cmd_no_lfeed(cmd):
return [str(x).rstrip("\n") for x in res_cmd_lfeed(cmd)]
def main():
cmd = ("ls -l")
print(res_cmd_no_lfeed(cmd))
if __name__ == '__main__':
main()
res_cmd_no_lfeed.py
#!/usr/bin/python
import subprocess
def res_cmd_lfeed(cmd): #The return value is not a list but each line
for line in subprocess.Popen(cmd, stdout=subprocess.PIPE,shell=True).stdout:
yield line
def res_cmd_no_lfeed(cmd):
return [str(x).rstrip("\n") for x in res_cmd_lfeed(cmd)]
def main():
cmd = ("ls -l")
print(res_cmd_no_lfeed(cmd))
if __name__ == '__main__':
main()
res_cmd_lfeed does not block until the end of the command, but returns a yield when it receives a line in the loop, that is, it stops the loop and returns the result at that time. If you are told "Next," it will resume and return the next result ... and so on.
In this example, res_cmd_no_lfeed waits until all the Arrays are completed, so it doesn't make sense. If it's a web application, you can steadily return a response to the browser.
forevertime.bat
@echo off
:TOP
echo %time%
goto top
You can see it by doing something like this. Since this batch file keeps outputting indefinitely, the former source keeps waiting with readlines (), but the latter captures and processes the result more and more. Like "Tear off and throw".
forevertime.bat(Mass output version)
@echo off
:TOP
set /P X=0123456789012345678901234567890123456789012345678901234567890123<NUL
set /P X=0123456789012345678901234567890123456789012345678901234567890123<NUL
set /P X=0123456789012345678901234567890123456789012345678901234567890123<NUL
set /P X=0123456789012345678901234567890123456789012345678901234567890123<NUL
set /P X=0123456789012345678901234567890123456789012345678901234567890123<NUL
set /P X=0123456789012345678901234567890123456789012345678901234567890123<NUL
set /P X=0123456789012345678901234567890123456789012345678901234567890123<NUL
set /P X=0123456789012345678901234567890123456789012345678901234567890123<NUL
echo %time%
goto top
I think I can tune more and more, but I wonder if I should leave it here.
Recommended Posts