Importthreading, OS, subprocess, Timeexec_path="/home/xhz/gems/ruby/amd...../bin/tester.exec"Out_data_path="/home/xhz/...generated/"max_process= 5Processes=set () Exec_commands= [] forIdinchXrange (1,45,2): Stat_filename= Out_data_path +'Stat_'+ str (ID) +'. txt'Config_filename= Out_data_path +'Config_'+ str (ID) +'. txt'Command="/home/xhz/gems/ruby.../tester.exec-l 1000000-i"+ STR (id*0.001) +"- F 1-d 2-x 4-j 0--stat_output_file"+ Stat_filename +"--config_output_file"+config_filename exec_commands.append (command) forCommandinchExec_commands:Printcommand Processes.add (subprocess. Popen (command, Shell=True)) whileLen (processes) >=max_process): Time.sleep (60) new_process=set () forPinchprocesses:PrintPif(P.poll () is notNone):#terminatedNew_processes.add (P)#Collect all threads that terminate
Processes.difference_update (new_process)#remove elements found in new_process
Reference Material:
From: http://zhou123.blog.51cto.com/4355617/1312791
Here are four ways Python executes a shell command:
1. The Os.system () function in the OS module executes the shell command
| 123 |
>>> os.system(‘ls‘)anaconda-ks.cfg install.log install.log.syslog send_sms_service.py sms.py0 |
Note that this method does not get the output of the shell command.
2, Popen () #这个方法能得到命令执行后的结果是一个字符串, to deal with their own to get the information you want.
| 12345 |
>>> import os>>> str=os.popen("ls").read()>>> a =str.split("\n")>>> forb ina: printb |
The result is the same as the first method.
3, Commands module # can be easily obtained command output (including standard and error output) and execution status bit
| 123456789101112 |
importcommandsa,b =commands.getstatusoutput(‘ls‘)a是退出状态b是输出的结果。>>> importcommands>>> a,b =commands.getstatusoutput(‘ls‘)>>> printa0>>> printbanaconda-ks.cfginstall.loginstall.log.syslog |
Commands.getstatusoutput (CMD) return (status,output)
Commands.getoutput (cmd) returns only output results
Commands.getstatus (file) returns the execution result string for the Ls-ld file, called GetOutput, and is not recommended for this method.
4. Subprocess Module
Using the Subprocess module, you can create new processes that can connect to the input/output/error pipelines of the new process and get the return status of the new process execution. The purpose of using the Subprocess module is to replace old functions or modules such as Os.system (), os.popen* (), commands.*, and so on.
Import subprocess
1, Subprocess.call (command, Shell=true)
#会直接打印出结果.
2, subprocess. Popen (command, shell=true) can also be subprocess. Popen (command, stdout=subprocess. PIPE, shell=true) so you can output the results.
If command is not an executable file, Shell=true is not allowed to be omitted.
Shell=true means the command is executed under the shell.
All four of these methods can execute shell commands.
Official website: Https://docs.python.org/2/library/subprocess.html?highlight=popen
At the same time sent a good blog: Http://blog.linuxeye.com/category/python
Some introduction to subprocess http://ipseek.blog.51cto.com/1041109/807513
Python subprocess Autorun Lab Program