python for循环并行处理-将数据追加到列表

txu3uszq  于 2022-12-05  发布在  Python
关注(0)|答案(1)|浏览(180)

我在我的代码中有下面的步骤,运行大约需要45到50分钟(还有其他步骤几乎不需要几秒钟)
因此,我尝试优化这一步的执行/运行时间,它本质上是函数内部的for循环

def getSwitchStatus(dashboard: meraki.DashboardAPI,switches): 
    statuses = [] 
    #Establish the timestamp for midnight yesterday to enable collecting of yesterdays data 
    yesterday_midnight = datetime.combine(datetime.today(), time.min) - timedelta(days = 1) 
    for dic in switches:
        statuses.append(dashboard.switch.getDeviceSwitchPortsStatuses(dic['serial'],t0=yesterday_midnight)) 
    return statuses

以下是我目前所尝试的方法

def switchsts():
    print("Inside switchsts")
    for dic in switches:
        statuses.append(dashboard.switch.getDeviceSwitchPortsStatuses(dic['serial'],t0=yesterday_midnight)) 

def getSwitchStatus(dashboard: meraki.DashboardAPI,switches): 
    print("Testing if switches is accessible")
    print("Switches type",type(switches))
    print("Switches",switches[0])

    p = Process(target=switchsts,args=())
    p.start()
    p.join()
    return statuses
    print(statuses)

很遗憾,这会在此处引发错误:

for dic in switches:

NameError:未定义名称'switches'
这很奇怪,因为当代码到达getswitchstatus函数内部时,我能够打印“开关”,但不知何故,我试图并行化的函数似乎没有读取它。
开关内部流程流程-1:
回溯(最近的呼叫排在最后):文件“C:\程序文件\Windows应用程序\Python软件基础. Python. 3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\multiprocessing\process.py”,第314行,在_引导程序self.run()文件“C:\程序文件\Windows应用程序\Python软件基础. Python. 3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\multiprocessing\process.py”,第108行,在运行self._target(*self._args,**self._kwargs)文件“C:\样本项目\venv\思科美拉基_文件_并行处理.py”,第83行,在用于dic的开关状态中:NameError:未定义名称'switches'
P.S.:我是并行处理的新手,所以我猜我犯了一些新手的错误

**Edit 1*加入'switches'的程式码

def getSwitches(dashboard: meraki.DashboardAPI,orgID, network_id=False): 
    if network_id is False or network_id is None: 
        devices = dashboard.organizations.getOrganizationDevices( 
            orgID, 
            total_pages='all', 
            productTypes='switch' 
        ) 
        return devices 
    else: 
        devices = dashboard.organizations.getOrganizationDevices( 
            orgID, 
            total_pages='all', 
            productTypes='switch', 
            networkIds=network_id 
        ) 
        return devices
zrfyljdw

zrfyljdw1#

它说明您的switchsts()函数中没有开关。
所以如果它有效的话,试试这个:

def switchsts(switches):
    print("Inside switchsts")
    for dic in switches:
        statuses.append(dashboard.switch.getDeviceSwitchPortsStatuses(dic['serial'],t0=yesterday_midnight)) 

def getSwitchStatus(dashboard: meraki.DashboardAPI,switches): 
    print("Testing if switches is accessible")
    print("Switches type",type(switches))
    print("Switches",switches[0])

    p = Process(target=switchsts, args=(switches,))
    p.start()
    p.join()
    return statuses
    print(statuses)

相关问题