使用多处理或线程脚本python

2024-09-30 06:18:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用多处理或线程的这段代码 我想控制线程示例threads=(50) 如果我选择50,则打开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,则打开50个进程


Tags: txtoutputifdefnotokoutping
1条回答
网友
1楼 · 发布于 2024-09-30 06:18:10

您可能希望使用来自pythonmultiprocessing模块的池对象。你知道吗

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

Pool object which offers a convenient means of parallelizing the execution of a function across multiple input values, distributing the input data across processes (data parallelism). The following example demonstrates the common practice of defining such functions in a module so that child processes can successfully import that module.

下面是一个简单的示例,演示ping调用的多处理。 希望这有帮助。你知道吗

编辑1

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

代码

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)]

相关问题 更多 >

    热门问题