Solved

Python multiprocessing licensing error

  • 1 August 2022
  • 3 replies
  • 169 views

Hi

 

I’m currently attempting to run some a series of Physical Optics Propagations in parallel, using pythonnet. They are pretty simple standalone codes which load in a lens file, carry out the propagation and output the coupling. I’ve been using multiprocessing/Pool and have an unusual issue when using too many cpus.

The computer I’m using has 16 cpus, but if I were to try and run more than 7 in parallel using Pool, I get a LicenseException (License is not valid for ZOSAPI use), though only for the 8th cpu.

I just wondered if you had any insight as to what could be causing this/different recommendations for running the API in parallel in Python.

 

Thanks!

icon

Best answer by MichaelH 1 August 2022, 17:52

View original

3 replies

Userlevel 6
Badge +2

This sounds like you’re actually establishing a new connection to OpticStudio on each thread rather than creating a new IOpticalSystem on each thread.  The ZOSAPI_Connection class requires a new license for each instance but multiple IOpticalSystem interfaces can be launched from the same ZOSAPI_Connection class (and thus only requiring a single license).  The Premium version of OpticStudio only allows 8 instances of OpticStudio (including ZOS-API and GUI versions), so my best guess if you’re running out of license when you try to launch your 8th instance of the ZOS-API (and I’m guessing you either have a GUI version running or another ZOS-API taking up your 8th license).

Try refactoring your multiprocessing/pool loops to simply launch new IOpticalSystems and you should be able to use all 16 CPUs.

Hi Michael

Ah okay that makes sense, thanks for the help there!

This perhaps is more a Python issue here, but I was having trouble with how to best approach launching multiple IOpticalSystems. The multiprocessing module generally doesn’t favour inputting zemax objects as a variable into the function for pool (where initially I thought simply passing in an IOpticalSystems object would work), so it seems I still need to initialise the system and analysis within the function itself.

To my knowledge however, to effectively ‘import’ the ZOSAPI module (to make the application, to then make the IOpticalSystems object), I need to launch the PythonStandaloneApplication, like in the top 2 lines below. This then carries the issue with the licensing again.

#Used to load/import local variables
zos = PythonStandaloneApplication()
ZOSAPI = zos.ZOSAPI

#Creating a new IOpticalSystems class for analysis
TheApplication = zos.TheApplication
syst = TheApplication.CreateNewSystem(ZOSAPI.SystemType.Sequential)
TheAnalyses = syst.Analyses
analyses = TheAnalyses.New_Analysis(ZOSAPI.Analysis.AnalysisIDM.PhysicalOpticsPropagation)

I hope that makes sense, any help is much appreciated!

Userlevel 3
Badge +2

Hi @tariqjh,

To clean up the connection after calling PythonStandaloneApplication(), you can use the del zos line. This will close down the server connection with OpticStudio and will release the license. Note that opening and closing this connection has performance implications, so if you can use the same connection as much as possible, that will improve efficiency.

Best,
Ethan 

Reply