私が書いているアプリをマルチスレッド化するために、concurrent.futures を使用しています。
netaddr から IPAddress をインポートしてアプリを起動します。
from netaddr import IPNetwork, IPAddress
次に、いくつかの入力ファイルを取得し、それらすべてをマルチスレッド化された関数に渡します。
with open(options.filename) as f:
contents = f.readlines()
executor = concurrent.futures.ProcessPoolExecutor(threads)
futures = [executor.submit(ip_compare, ip, scope_list) for ip in contents]
次に、結果が完了するのを待ち、それらを出力変数に追加します。
for future in concurrent.futures.as_completed(futures):
output.append(future.results()
私が抱えている問題は、将来からの例外を取得し続けることです:
global name 'IPAddress' is not defined
ip_compare 関数は次のとおりです。
def ip_compare(ip_addr, scope_list):
ip_addr = ip_addr.rstrip()
if not is_ipv4(ip_addr):
try:
ip = socket.gethostbyname(ip_addr)
except:
return "error," + ip_addr + ",,," + str(sys.exc_info()[0]).replace(',',';') + "\r\n"
else:
ip = ip_addr
for scope in scope_list:
if IPAddress(ip) in IPNetwork(scope):
return "in," + ip_addr + "," + ip + "," + scope + ",\r\n"
return "out," + ip_addr + "," + ip + "," + ",,\r\n"
先物がロードされたモジュールを認識しない理由は何ですか?
エラーのために IDE がスクリプトの実行を停止すると、メモリに IPAddress が定義されていることがはっきりとわかります。
IPAddress = {type} <class 'netaddr.ip.IPAddress'>