I am have below step in my code which is taking around 45 to 50 mins to run (there are other steps which barely take few seconds)
So I am trying to optimize the execution/run time for this step it is essentially a for loop inside a function
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
Here is what I have tried to do so far
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)
Unfortunately this is throwing an error here:
for dic in switches:
NameError: name 'switches' is not defined
Which is strange because I am able to print 'Switches' when the code reaches inside the getswitchstatus function but somehow the function that I am trying to parallelize doesnt seem to read it.
Inside switchsts Process Process-1:
Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\multiprocessing\process.py", line 314, in _bootstrap self.run() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\multiprocessing\process.py", line 108, in run self._target(*self._args, **self._kwargs) File "C:\Sample_project\venv\ciscomeraki_file_parallelprocessing.py", line 83, in switchsts for dic in switches: NameError: name 'switches' is not defined
P.S.: I am new to parellel processing so I am guessing I am making some rookie mistake
*Edit1 Adding code for '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
Pool
? (The question isn't clear enough for me to decide if that would help.)