1. import threading, os, subprocess, time
  2.  
  3. exec_path = "/home/xhz/gems/ruby/amd...../bin/tester.exec"
  4.  
  5. out_data_path = "/home/xhz/...generated/"
  6.  
  7. max_process = 5
  8.  
  9. processes = set()
  10.  
  11. exec_commands = []
  12.  
  13. for id in xrange(1,45,2):
  14. stat_filename = out_data_path + 'stat_' + str(id) + '.txt'
  15. config_filename = out_data_path + 'config_' + str(id) + '.txt'
  16. 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
  17. exec_commands.append(command)
  18.  
  19. for command in exec_commands:
  20. print command
  21. processes.add(subprocess.Popen(command, shell=True))
  22. while len(processes) >= max_process):
  23. time.sleep(60)
  24. new_process = set()
  25. for p in processes:
  26. print p
  27. if(p.poll() is not None): #terminated
  28. new_processes.add(p) #collect all threads that terminate
  29.  
  30. processes.difference_update(new_process) #remove elements found in new_process

参考材料:

来自:http://zhou123.blog.51cto.com/4355617/1312791

这里介绍一下python执行shell命令的四种方法:

1、os模块中的os.system()这个函数来执行shell命令

1
2
3
>>> os.system('ls')
anaconda-ks.cfg  install.log  install.log.syslog  send_sms_service.py  sms.py
0

注,这个方法得不到shell命令的输出。

2、popen()#这个方法能得到命令执行后的结果是一个字符串,要自行处理才能得到想要的信息。

1
2
3
4
5
>>> import os
>>> str = os.popen("ls").read()
>>> a = str.split("\n")
>>> for in a:
        print b

这样得到的结果与第一个方法是一样的。

3、commands模块#可以很方便的取得命令的输出(包括标准和错误输出)和执行状态位

1
2
3
4
5
6
7
8
9
10
11
12
import commands
a,b = commands.getstatusoutput('ls')
a是退出状态
b是输出的结果。
>>> import commands
>>> a,b = commands.getstatusoutput('ls')
>>> print a
0
>>> print b
anaconda-ks.cfg
install.log
install.log.syslog

commands.getstatusoutput(cmd)返回(status,output)

commands.getoutput(cmd)只返回输出结果

commands.getstatus(file)返回ls -ld file 的执行结果字符串,调用了getoutput,不建议使用这个方法。

4、subprocess模块

使用subprocess模块可以创建新的进程,可以与新建进程的输入/输出/错误管道连通,并可以获得新建进程执行的返回状态。使用subprocess模块的目的是替代os.system()、os.popen*()、commands.*等旧的函数或模块。

import subprocess

1、subprocess.call(command, shell=True)

#会直接打印出结果。

2、subprocess.Popen(command, shell=True) 也可以是subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) 这样就可以输出结果了。

如果command不是一个可执行文件,shell=True是不可省略的。

shell=True意思是shell下执行command

这四种方法都可以执行shell命令。

官网:https://docs.python.org/2/library/subprocess.html?highlight=popen

同时发了一个好的博客:http://blog.linuxeye.com/category/python

关于subprocess 的一些介绍 http://ipseek.blog.51cto.com/1041109/807513

python subprocess 自动运行实验室程序的更多相关文章

  1. java testng框架的windows自动化-自动运行testng程序上篇

    本文旨在让读者简单了解testng的自动运行 怎么说呢,在网上已经有了各个前辈进行代码演示以及分享,我力争说到点子上 接上文,之前讲的大部分是juint的自动化代码运行,从未涉及到testng,但是在 ...

  2. NanoPC-T4/RK3399开发板Ubuntu FriendlyCore系统开机自动运行客户程序

    RK3399开机自动运行客户程序 比如hellohello.c 交叉编译:aarch64-linux-gcc hello.c -o hello使用SecureCRT软件通过串口下载到开发板rz修改文件 ...

  3. python实现自动重启本程序的方法 技术的漩涡

    python实现自动重启本程序的方法 http://www.jb51.net/article/69174.htm import requests, time url_l = []with open(' ...

  4. java juint框架的windows自动化-自动运行juint程序简述

    在京东混了一个月,基本有点稳定了,觉得也有所余力了现在,继续写博客吧,不过以后更新也许不是那么频繁了 本人使用的是juint框架,对开发是一个单元测试的java框架,但是对测试而言是java的基石之一 ...

  5. 在Linux中以普通用户开机自动运行脚本程序

    测试环境:CentOS6.5 管理员:root 普通用户:test1 实现目标:在Linux启动时,以普通用户test1自动运行位于根目录下的脚本程序test.py,该程序会在每次执行时自动向本地日志 ...

  6. java的mac自动化-自动运行java程序

    本文旨在帮助读者介绍,如果一个测试工程师拿到了mac本,该如何在本地自动运行java代码 首先如图所示写下如下一段代码 package zlr;import org.junit.Test;public ...

  7. java的windows自动化-自动运行java程序

    那么在一些工具齐全并且已经有了一定的写好的java程序的情况下(环境变量和软件见上一章http://www.cnblogs.com/xuezhezlr/p/7718273.html),如何自动化运行j ...

  8. java testng框架的windows自动化-自动运行testng程序下篇

    本文旨在让读者简单了解testng的自动运行 接上文https://www.cnblogs.com/xuezhezlr/p/9213456.html,文章大致把testng中比较特殊的两个xml形式说 ...

  9. python在mapreduce运行Wordcount程序

    首先脚本文件: mapper.py: #!/usr/bin/env python import sys for line in sys.stdin: line = line.strip() words ...

随机推荐

  1. mobx源码解读2

    我们将上节用到的几个类的构造器列举一下吧: function Reaction(name, onInvalidate) { if (name === void 0) { name = "Re ...

  2. LinQ的增删改查

    全名:LinQ to sql类:集成化的数据访问类.会自动生成,进行数据库数据访问. LinQ的创建: 1.链接数据库:添加--添加新项--找到LINQ to SQL类--名字就叫数据库的名称就好. ...

  3. {POJ}{3971}{Scales}{O(N)动态规划}

    题意:给定一堆2二进制砝码,给定一个物品,要求在天平两端加入物品和砝码使之平衡,求可能数. 思路:一开始想到了直接用数学原理,结果没证出来.做如下思考,此题需要用二进制: (1)设物品重量为w,加入的 ...

  4. mysql使用小技巧

    1.mySql 删除表中大批量的数据 假设有一个表(logs)有2000万条记录,我们要在业 务不停止的情况下删除其中status=1的所有记录,差不多有1800万条,直接执行 DELETE FROM ...

  5. adb opendir failed ,permission denied

    做数据库的时候,我手机是htc的,root过的,找数据库db文件一直找不到, 我使用的adb命令ls的时候会提示:adb opendir failed ,permission denied ,解决方法 ...

  6. select 选中 option的问题

    1.[可以实现 不推荐  适合多选] $("#organize_type").find("option:eq("+j+")").attr(& ...

  7. DIOCP之编写第一个应用程序(二)

    构建client界面: 构建界面要比写代码更难爱,不是专业UI设计太丑,先有个界面,好写代码,客户端代码与界面设计思想:界面与数据之间分离处理,不能要接收数据的地方写代码,不然以后修改程序会死人的.

  8. 设置centos7语言显示环境

    1.查看可选语言显示包 locale -a ............(省略好多) zh_CNzh_CN.gb18030zh_CN.gb2312zh_CN.gbkzh_CN.utf8zh_HKzh_HK ...

  9. vim安装不上

    前阵子,刚安装Ubuntu时,安装vim的问题,现在些出来分享一下.apt-get install vim正在读取软件包列表... 完成正在分析软件包的依赖关系树正在读取状态信息... 完成有一些软件 ...

  10. perl中常见的语法规则和函数

    数值比较操作符         字符串 相等          ==                        eq 不等          !=                         ...