1
我正在使用concurrent.futures多线程编写我正在编写的应用程序。Concurrent.futures - 返回导入模块未定义的错误
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]
那我等得到
我从netaddr中输入ip地址启动应用程序结果完成并将它们追加到输出变量:
for future in concurrent.futures.as_completed(futures):
output.append(future.results()
我遇到的问题是t帽子我不断收到来自未来EXCETION:
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停止脚本的执行,因为错误的,我可以清楚地看到,ip地址是在内存中定义:
IPAddress = {type} <class 'netaddr.ip.IPAddress'>
Per [the docs](https://docs.python.org/3/library/concurrent.futures.html#processpoolexecutor),*“__main__'模块必须可由工作程序子进程导入,这意味着ProcessPoolExecutor在交互式解释器中不起作用。“*这意味着每个工作者子过程输入'__main__'模块。因此'if __name__ =='__main __''之外的全局变量被定义。 'if语句'中的那些不会被执行。 – unutbu