zoukankan      html  css  js  c++  java
  • python subprocess 自动运行实验室程序

    import threading, os, subprocess, time
    
    exec_path = "/home/xhz/gems/ruby/amd...../bin/tester.exec"
    
    out_data_path = "/home/xhz/...generated/"
    
    max_process = 5
    
    processes = set()
    
    exec_commands = []
    
    for id in xrange(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)
    
    for command in exec_commands:
        print command
        processes.add(subprocess.Popen(command, shell=True))
        while len(processes) >= max_process):
            time.sleep(60)
            new_process = set()
            for p in processes:
                print p
                if(p.poll() is not None): #terminated
                    new_processes.add(p)      #collect all threads that terminate

    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(" ")
    >>> 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

  • 相关阅读:
    Linux useradd 命令介绍
    lsscsi
    安装MegaCli,查看linux服务器raid信息
    ipmitool命令详解
    python 收发邮件
    angularjs 高级玩法 创建递归的模板
    我的Android进阶之旅------>Android Activity的singleTask载入模式和onActivityResult方法之间的冲突
    Git实战(三)环境搭建
    使用Samba实现Linux与Windows文件共享实践
    设计模式个人备忘(享元模式,strategy, templete strategy)
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3966922.html
Copyright © 2011-2022 走看看