다음은 코드입니다. 계획은 범위에있는 모든 호스트를 핑 (ping)하여 fqdn을 다시 얻는 것입니다 ... 작동하지만, 시간이 오래 걸립니다 .... 그래서 멀티 프로세싱에 대해 생각합니다 ... 그러나 작동하지 않습니다. 코드는 p.map (...)에서 멈추고 그 이상으로 넘어 가지 않습니다. 누군가가 도움을 주시겠습니까? ............. 코드를 읽을 수있는 희망 ..멀티 스레드 딜레마
import subprocess
import os
import socket
import multiprocessing
from multiprocessing.dummy import Pool as ThreadPool
from datetime import datetime
range_85 = "10.85.40"
range_87 = "10.87.40"
print ("starting")
replyfile = open("pingdns_replies.txt", "a")
#replyfile.write("START %s \n" % datetime.now())
print("file opened")
def ping(host):
print ("entered function")
#for ip in range (50,250):
#host = range_85 +"."+ str(ip)
print (host)
rep = subprocess.call("ping %s" % host, shell=False)
if rep == 0:
try:
dns = socket.getfqdn(host)
except:
dns = " "
print ("%s at %s is up" % (dns, host))
replyfile.write(" %s at %s is up \n" % (dns, host))
else:
try:
dns = socket.getfqdn(host)
print("%s at %s is down ?" % (dns, host))
replyfile.write(" %s at %s is down ? \n" % (dns,host))
except:
dns = " "
print ("%s received no reply" % host)
replyfile.write(" %s received no reply \n" % (dns, host))
#ips = (range_85 + "." + str(i) for i in range(0,255))
ips = []
for i in range(1,255):
ips.append(range_85+'.'+str(i))
print ("ips created")
print (ips)
with multiprocessing.Pool(2) as p:
p.map(ping, ips)
replyfile.write("END %s \n" % datetime.now())
replyfile.close()
코드를 편집하여 읽기 쉽게하십시오. – chrisz
http://idownvotedbecau.se/nomcve/ –
'ping'을 서브 프로세스로 호출하는 경우 다중 처리가 필요하지 않습니다. 이미 여러 프로세스를 사용하고 있습니다. –