使用多处理或线程脚本python

时间:2018-03-29 01:36:45

标签: python multithreading

我希望对此代码使用多处理或线程 我想控制线程示例threads =(50) 如果我选择50 theards是opend 50进程 请帮我 这段代码: -

import subprocess
import csv

def ping(hostname):
    p = subprocess.Popen(["ping", "-n", "1","-w","1000",hostname], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    pingStatus = 'ok';

    for line in p.stdout:
        output = line.rstrip().decode('UTF-8')

        if (output.endswith('unreachable.')) :
            #No route from the local system. Packets sent were never put on the wire.
            pingStatus = 'unreacheable'
            break
        elif (output.startswith('Ping request could not find host')) :
            pingStatus = 'host_not_found'
            break
        if (output.startswith('Request timed out.')) :
            #No Echo Reply messages were received within the default time of 1 second.
            pingStatus = 'timed_out'
            break
        #end if
    #endFor

    return pingStatus
#endDef    


def printPingResult(hostname):
    statusOfPing = ping(hostname)

    if (statusOfPing == 'host_not_found') :
        writeToFile('!server-not-found.txt', hostname)
    elif (statusOfPing == 'unreacheable') :
       writeToFile('!unreachable.txt', hostname)
    elif (statusOfPing == 'timed_out') :
       writeToFile('!timed_out.txt', hostname)     
    elif (statusOfPing == 'ok') :
        writeToFile('!ok.txt', hostname)
    #endIf
#endPing


def writeToFile(filename, data) :
    with open(filename, 'a') as output:
        output.write(data + '\n')
    #endWith
#endDef    


'''
servers.txt example
   vm8558
   host2
   server873
   google.com
'''
file = open('hosts.txt')

try:
    reader = csv.reader(file)

    for item in reader:
        printPingResult(item[0].strip())
    #endFor
finally:
    file.close()
#endTry

我希望对此代码使用多处理或线程 我想控制线程示例threads =(50) 如果我选择50 theards是opend 50 process

1 个答案:

答案 0 :(得分:1)

您可能希望使用python multiprocessing模块中的池对象。

来自https://docs.python.org/2/library/multiprocessing.html

  

池对象,它提供了跨多个输入值并行处理函数执行的便捷方法,跨进程分配输入数据(数据并行)。以下示例演示了在模块中定义此类函数的常见做法,以便子进程可以成功导入该模块。

这是一个演示ping调用的多处理的简单示例。 希望这会有所帮助。

<强> EDIT1

**host.txt**
google.com
yahoo.com
microsoft.com
cnn.com
stackoverflow.com
github.com

<强> CODE

from multiprocessing import Pool 
import subprocess

def ping(hostname):
    return hostname, subprocess.call(['ping', '-c', '3', '-w', '1000', hostname])

if __name__ == '__main__':
    HOSTFILE = 'server.txt'
    POOLCOUNT = 5
    #read host name file and load to list
    hostfile = open(HOSTFILE, 'r')
    hosts = [line.strip() for line in hostfile.readlines()]

    #Create pool    
    p = Pool(POOLCOUNT)

    #multiprocess and map ping function to host list
    print(p.map(ping, hosts))

<强>结果

状态1 =成功,0 =失败

>>> 
[('google.com', 1), ('yahoo.com', 1), ('microsoft.com', 1), ('cnn.com', 1), ('stackoverflow.com', 1), ('github.com', 1)]