【发布时间】:2019-08-22 16:55:16
【问题描述】:
我有一些包含数据的大型列表,需要处理。我正在使用多处理来更快地完成工作,除非列表包含超过 100 行,否则它工作得很好,因为它开始使用大量 CPU 和内存。
我提供了一些示例数据:
changed_devices = [[3036c360, 013-HX.ITEM-UA-01, DESCRIPTION 6, 172.29.6.2, Location#All Locations#013-LOCATION-1, IPSEC#Is IPSEC Device#No, Device Type#All Device Types#Switch, ISE Config#ISE Config#With ISE Config Monitor Mode, ISE_enable#ISE_enable#YES - Monitor Mode],
[306fd4c0, 013-HX.ITEM-UA-02, DESCRIPTION 7, 172.29.6.3, Location#All Locations#013-LOCATION-1, IPSEC#Is IPSEC Device#No, Device Type#All Device Types#Switch, ISE Config#ISE Config#With ISE Config Monitor Mode, ISE_enable#ISE_enable#YES - Monitor Mode],
[30a8bf10, 013-UX.ITEM-UA-01, DESCRIPTION 8, 172.29.6.10, Location#All Locations#013-LOCATION-1, IPSEC#Is IPSEC Device#No, Device Type#All Device Types#Switch, ISE Config#ISE Config#With ISE Config Monitor Mode, ISE_enable#ISE_enable#YES - Monitor Mode],
[30e293c0, 013-UX1.ITEM-UA-01, DESCRIPTION 1, 172.29.6.4, Location#All Locations#013-LOCATION-1, IPSEC#Is IPSEC Device#No, Device Type#All Device Types#Switch, ISE Config#ISE Config#With ISE Config Monitor Mode, ISE_enable#ISE_enable#YES - Monitor Mode],
[311cb690, 013-UX2.ITEM-UA-01, DESCRIPTION 2, 172.29.6.5, Location#All Locations#013-LOCATION-1, IPSEC#Is IPSEC Device#No, Device Type#All Device Types#Switch, ISE Config#ISE Config#With ISE Config Monitor Mode, ISE_enable#ISE_enable#YES - Monitor Mode],
[3155ef00, 013-UX3.ITEM-UA-01, DESCRIPTION 3, 172.29.6.6, Location#All Locations#013-LOCATION-1, IPSEC#Is IPSEC Device#No, Device Type#All Device Types#Switch, ISE Config#ISE Config#With ISE Config Monitor Mode, ISE_enable#ISE_enable#YES - Monitor Mode],
[318ed950, 013-UX4.ITEM-UA-01, DESCRIPTION 4, 172.29.6.7, Location#All Locations#013-LOCATION-1, IPSEC#Is IPSEC Device#No, Device Type#All Device Types#Switch, ISE Config#ISE Config#With ISE Config Monitor Mode, ISE_enable#ISE_enable#YES - Monitor Mode],
[31c8d510, 013-UX5.ITEM-UA-01, DESCRIPTION 5, 172.29.6.8, Location#All Locations#013-LOCATION-1, IPSEC#Is IPSEC Device#No, Device Type#All Device Types#Switch, ISE Config#ISE Config#With ISE Config Monitor Mode, ISE_enable#ISE_enable#YES - Monitor Mode],
[3202f7e0, 013-UX5.ITEM-UA-02, DESCRIPTION 5, 172.29.6.9, Location#All Locations#013-LOCATION-1, IPSEC#Is IPSEC Device#No, Device Type#All Device Types#Switch, ISE Config#ISE Config#With ISE Config Monitor Mode, ISE_enable#ISE_enable#YES - Monitor Mode]]
我当前的代码:
if __name__ == "__main__":
for chg in range(len(changed_devices)):
p = multiprocessing.Process(target=start_exscript, args=(accounts,changed_devices[chg][0],changed_devices[chg][1],changed_devices[chg][2],changed_devices[chg][3],changed_devices[chg][4],changed_devices[chg][5],changed_devices[chg][6],changed_devices[chg][7],changed_devices[chg][8],changed_devices[chg][9],changed_devices[chg][10],))
processes.append(p)
p.start()
for process in processes:
process.join()
列表可以包含超过 5000 个条目,所以我希望脚本能够解决这个问题,并且一次只能启动 100 个线程。有人有想法吗?
更新:
我仍然无法让它工作,所以我会提供一些额外的细节。
我使用的是 Python 2.7,但无法让 concurrent.futures 工作。
start_exscript 只是另一个函数,它看起来像这样:
def start_exscript(accounts, host, ena, ise_nwdvs_id, ise_nwdvs_name, ise_nwdvs_ip, ise_nwdvs_desc, ise_nwdvs_loc, ise_nwdvs_ipsec, ise_nwdvs_type, ise_nwdvs_cfg, ise_nwdvs_ena):
try:
start(accounts, host, bind(update_config_cisco_device, ena), verbose=0) # starts Python Exscript
updateisedvsdata = [ise_nwdvs_id, ise_nwdvs_name, ise_nwdvs_ip, ise_nwdvs_desc, ise_nwdvs_loc, ise_nwdvs_ipsec, ise_nwdvs_type, ise_nwdvs_cfg, ise_nwdvs_ena]
ise_update_nw_dvs = ise_update_network_device(updateisedvsdata) # Start update function
if ise_update_nw_dvs:
logger.critical("[ROOT]: Updating ISE config variable on: [ "+ise_nwdvs_name+" ] [ OK ]")
else:
logger.critical("[ROOT]: Updating ISE config variable on: [ "+ise_nwdvs_name+" ] [ Failed ]")
except Exception as e:
logger.critical(e.message, e.args)
Python Exscript只是一个可以登录网络设备的插件。
我在使用 Pool 传递所有参数时遇到问题。
我试过了,但它不会启动:
cpus = multiprocessing.cpu_count() - 1
pool = Pool(processes=cpus)
for chg in range(len(changed_devices)):
pool.apply_async(start_exscript,args=(accounts,changed_devices[chg][0],changed_devices[chg][1],changed_devices[chg][2],changed_devices[chg][3],changed_devices[chg][4],changed_devices[chg][5],changed_devices[chg][6],changed_devices[chg][7],changed_devices[chg][8],changed_devices[chg][9],changed_devices[chg][10]))
pool.close()
pool.join()
这没有任何反应。我什至没有收到错误,这让我更加困惑。
更新 2
任何向外看的人的最终工作结果。非常感谢 SyntaxVoid。
def start_exscript(args):
try:
# --- PLEASE READ ---
# Setup Switch/Router CLI access - remember to change
cliuser = "x"
clipass = "x"
accounts = Account(cliuser,clipass,needs_lock=False)
host = args[0]
ena = args[1]
ise_nwdvs_id = args[2]
ise_nwdvs_name = args[3]
ise_nwdvs_ip = args[4]
ise_nwdvs_desc = args[5]
ise_nwdvs_loc = args[6]
ise_nwdvs_ipsec = args[7]
ise_nwdvs_type = args[8]
ise_nwdvs_cfg = args[9]
ise_nwdvs_ena = args[10]
start(accounts, host, bind(update_config_cisco_device, ena), verbose=0) # Start Exscript process
updateisedvsdata = [ise_nwdvs_id, ise_nwdvs_name, ise_nwdvs_ip, ise_nwdvs_desc, ise_nwdvs_loc, ise_nwdvs_ipsec, ise_nwdvs_type, ise_nwdvs_cfg, ise_nwdvs_ena] # Update data
ise_update_nw_dvs = ise_update_network_device(updateisedvsdata) # Start update process
if ise_update_nw_dvs: # If update went well
logger.critical("[ROOT]: Updating ISE config variable on: [ "+ise_nwdvs_name+" ] [ OK ]")
else:
logger.critical("[ROOT]: Updating ISE config variable on: [ "+ise_nwdvs_name+" ] [ Failed ]")
except Exception as e:
logger.critical(e.message, e.args)
if __name__ == "__main__":
cpus = multiprocessing.cpu_count() - 1
pool = Pool(processes=cpus)
results = pool.map(start_exscript, changed_devices)
【问题讨论】:
-
您可能希望实现一个由 100 个进程组成的池,只要有机会就从队列中提取参数。这样一来,最多只能运行 100 个进程。
-
@SyntaxVoid 听起来是个不错的计划,我该怎么做?你能举个例子吗?
-
仍未得到答复,我希望有人能对此有所了解。请参阅更新部分。
标签: python-2.7