Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deleting the ZOS instance doesn't (always) work #61

Open
4 of 5 tasks
crnh opened this issue Jan 11, 2024 · 2 comments
Open
4 of 5 tasks

Deleting the ZOS instance doesn't (always) work #61

crnh opened this issue Jan 11, 2024 · 2 comments
Assignees

Comments

@crnh
Copy link
Collaborator

crnh commented Jan 11, 2024

Problem

Several users of ZOSPy complained about being unable to create a new ZOS instance, even after deleting the existing instance using del zos (being unable to create a new instance when another instance exists is by design).
I looked into this and there are two main reasons for this problem:

  1. The use of an interactive Python environment like IPython or Jupyter notebooks. These environments may keep some references to variables even after deleting them, so the existing ZOS instance is not removed by the garbage collector after calling del zos.
  2. The existence of OpticStudioSystem instances. Every OpticStudioSystem contains a reference to the ZOS instance that created it. As a result, there will still be some references to this ZOS instance, and deleting zos will not result in its removal by the garbage collector. It is therefore necessary to

I am not sure if this should be considered a bug, as this is related to how Python works. Furthermore, it is not necessary to delete and recreate ZOS objects, as the same instance can be used to manage all OpticStudioSystem instances. Nevertheless, users expect to be able to delete ZOS (probably due to the OpticStudio documentation suggesting to do so) so we should at least consider adding a note about this behaviour to the documentation. It may also be possible to automatically delete all OpticStudioSystem instances when deleting zos, but this most likely involves messing with the garbage collector, which may be dangerous.

What version of ZOSPy are you running?

1.2.0

What version of OpticStudio are you running?

23R2

Which operating system do you use?

Windows 10 Enterprise

In which environment do you use ZOSPy?

  • Plain Pythons scripts (not in Spyder)
  • Jupyter notebooks
  • Spyder

In which connection modes does the problem occur?

  • Extension mode
  • Standalone mode

Example code

import logging

import zospy as zp

# Initiate logging so we can follow what ZOSPy is doing
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")

zos = zp.ZOS()
oss = zos.connect(mode="extension")

del zos

# See the output below: if this doesn't print the empty set, the instance was not deleted
print(f"ZOS instances: {zp.ZOS._instances}")

Output

2024-01-11 12:46:33,917 - zospy.zpcore - DEBUG - Loading ZOS DLLs with preload set to False
2024-01-11 12:46:33,917 - zospy.api.apisupport - INFO - Obtaining Zemax Location from Windows Registry
2024-01-11 12:46:33,917 - zospy.api.apisupport - INFO - Found ZemaxFolder at XXX
2024-01-11 12:46:33,917 - zospy.api.apisupport - DEBUG - Closing registry key
2024-01-11 12:46:33,917 - zospy.api.apisupport - DEBUG - Registry key closed
2024-01-11 12:46:33,917 - zospy.api.apisupport - DEBUG - Adding reference XXX to clr
2024-01-11 12:46:33,933 - zospy.api.apisupport - DEBUG - Importing ZOSAPI_NetHelper
2024-01-11 12:46:33,935 - zospy.api.apisupport - INFO - ZOSAPI_NetHelper imported successfully
2024-01-11 12:46:33,935 - zospy.api.apisupport - INFO - Obtaining Zemax Directory from ZOSAPI_NetHelper
2024-01-11 12:46:33,946 - zospy.api.apisupport - INFO - Zemax OpticStudio found at XXX
2024-01-11 12:46:33,946 - zospy.api.apisupport - INFO - Searching and registering ZOSAPI DLLs
2024-01-11 12:46:33,946 - zospy.api.apisupport - DEBUG - ZOSAPI_Interfaces.dll found
2024-01-11 12:46:33,959 - zospy.api.apisupport - INFO - ZOSAPI_Interfaces imported to clr
2024-01-11 12:46:33,960 - zospy.api.apisupport - DEBUG - ZOSAPI.dll found
2024-01-11 12:46:33,963 - zospy.api.apisupport - INFO - ZOSAPI imported to clr
2024-01-11 12:46:33,963 - zospy.api.apisupport - DEBUG - Checking content of ZOSAPI_Interfaces.dll
2024-01-11 12:46:33,976 - zospy.api.apisupport - DEBUG - Loading ZOSAPI
2024-01-11 12:46:33,978 - zospy.api.apisupport - DEBUG - ZOSAPI loaded
2024-01-11 12:46:33,978 - zospy.api.apisupport - DEBUG - Loading nested namespaces
2024-01-11 12:46:33,978 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis preloaded
2024-01-11 12:46:33,978 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Data preloaded
2024-01-11 12:46:33,978 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.PhysicalOptics preloaded
2024-01-11 12:46:33,978 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.RayTracing preloaded
2024-01-11 12:46:33,978 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings preloaded
2024-01-11 12:46:33,978 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Aberrations preloaded
2024-01-11 12:46:33,979 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.DiffractionEfficiency preloaded
2024-01-11 12:46:33,979 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.EncircledEnergy preloaded
2024-01-11 12:46:33,979 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.ExtendedScene preloaded
2024-01-11 12:46:33,979 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Fans preloaded
2024-01-11 12:46:33,979 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Materials preloaded
2024-01-11 12:46:33,979 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Mtf preloaded
2024-01-11 12:46:33,979 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.NSCSurface preloaded
2024-01-11 12:46:33,979 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Psf preloaded
2024-01-11 12:46:33,979 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.RMS preloaded
2024-01-11 12:46:33,980 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.RMS.RMSField preloaded
2024-01-11 12:46:33,980 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.RMS.RMSFieldMap preloaded
2024-01-11 12:46:33,980 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.RayTracing preloaded
2024-01-11 12:46:33,980 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Spot preloaded
2024-01-11 12:46:33,980 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Surface preloaded
2024-01-11 12:46:33,980 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Wavefront preloaded
2024-01-11 12:46:33,980 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Tolerancing preloaded
2024-01-11 12:46:33,980 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Tolerancing.QuickYield preloaded
2024-01-11 12:46:33,980 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Common preloaded
2024-01-11 12:46:33,981 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Editors preloaded
2024-01-11 12:46:33,981 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Editors.LDE preloaded
2024-01-11 12:46:33,981 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Editors.MCE preloaded
2024-01-11 12:46:33,981 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Editors.MFE preloaded
2024-01-11 12:46:33,981 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Editors.NCE preloaded
2024-01-11 12:46:33,981 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Editors.TDE preloaded
2024-01-11 12:46:33,981 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Preferences preloaded
2024-01-11 12:46:33,981 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.SystemData preloaded
2024-01-11 12:46:33,981 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools preloaded
2024-01-11 12:46:33,981 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools.FileManager preloaded
2024-01-11 12:46:33,982 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools.General preloaded
2024-01-11 12:46:33,982 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools.LMx preloaded
2024-01-11 12:46:33,983 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools.Optimization preloaded
2024-01-11 12:46:33,983 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools.RayTrace preloaded
2024-01-11 12:46:33,983 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools.Tolerancing preloaded
2024-01-11 12:46:33,983 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Wizards preloaded
2024-01-11 12:46:34,145 - zospy.zpcore - DEBUG - Assigning ZOSAPI_Connection() to self.Connection
2024-01-11 12:46:34,476 - zospy.zpcore - DEBUG - ZOSAPI_Connection() already assigned self.Connection
ZOS instances: {2341316532496} <-- Not an empty set, so the instance has not been deleted
2024-01-11 12:46:35,024 - zospy.zpcore - DEBUG - Closing connections with Zemax OpticStudio

Additional information

When oss is deleted as well, the ZOS instance is correctly deleted:

import logging

import zospy as zp

# Initiate logging so we can follow what ZOSPy is doing
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")

zos = zp.ZOS()
oss = zos.connect(mode="extension")

del oss
del zos

# See the output below: if this doesn't print the empty set, the instance was not deleted
print(f"ZOS instances: {zp.ZOS._instances}")

Output:

2024-01-11 12:49:02,026 - zospy.zpcore - DEBUG - Initializing ZOS instance
2024-01-11 12:49:02,027 - zospy.zpcore - INFO - ZOS instance initialized
2024-01-11 12:49:02,027 - zospy.zpcore - DEBUG - Loading ZOS DLLs with preload set to False
2024-01-11 12:49:02,027 - zospy.api.apisupport - INFO - Obtaining Zemax Location from Windows Registry
2024-01-11 12:49:02,027 - zospy.api.apisupport - INFO - Found ZemaxFolder at XXX
2024-01-11 12:49:02,027 - zospy.api.apisupport - DEBUG - Closing registry key
2024-01-11 12:49:02,027 - zospy.api.apisupport - DEBUG - Registry key closed
2024-01-11 12:49:02,027 - zospy.api.apisupport - DEBUG - Adding reference XXX to clr
2024-01-11 12:49:02,041 - zospy.api.apisupport - DEBUG - Importing ZOSAPI_NetHelper
2024-01-11 12:49:02,042 - zospy.api.apisupport - INFO - ZOSAPI_NetHelper imported successfully
2024-01-11 12:49:02,042 - zospy.api.apisupport - INFO - Obtaining Zemax Directory from ZOSAPI_NetHelper
2024-01-11 12:49:02,052 - zospy.api.apisupport - INFO - Zemax OpticStudio found at XXX
2024-01-11 12:49:02,052 - zospy.api.apisupport - INFO - Searching and registering ZOSAPI DLLs
2024-01-11 12:49:02,053 - zospy.api.apisupport - DEBUG - ZOSAPI_Interfaces.dll found
2024-01-11 12:49:02,065 - zospy.api.apisupport - INFO - ZOSAPI_Interfaces imported to clr
2024-01-11 12:49:02,066 - zospy.api.apisupport - DEBUG - ZOSAPI.dll found
2024-01-11 12:49:02,068 - zospy.api.apisupport - INFO - ZOSAPI imported to clr
2024-01-11 12:49:02,068 - zospy.api.apisupport - DEBUG - Checking content of ZOSAPI_Interfaces.dll
2024-01-11 12:49:02,081 - zospy.api.apisupport - DEBUG - Loading ZOSAPI
2024-01-11 12:49:02,083 - zospy.api.apisupport - DEBUG - ZOSAPI loaded
2024-01-11 12:49:02,083 - zospy.api.apisupport - DEBUG - Loading nested namespaces
2024-01-11 12:49:02,083 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis preloaded
2024-01-11 12:49:02,083 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Data preloaded
2024-01-11 12:49:02,083 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.PhysicalOptics preloaded
2024-01-11 12:49:02,083 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.RayTracing preloaded
2024-01-11 12:49:02,083 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings preloaded
2024-01-11 12:49:02,083 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Aberrations preloaded
2024-01-11 12:49:02,083 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.DiffractionEfficiency preloaded
2024-01-11 12:49:02,084 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.EncircledEnergy preloaded
2024-01-11 12:49:02,084 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.ExtendedScene preloaded
2024-01-11 12:49:02,084 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Fans preloaded
2024-01-11 12:49:02,084 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Materials preloaded
2024-01-11 12:49:02,084 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Mtf preloaded
2024-01-11 12:49:02,084 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.NSCSurface preloaded
2024-01-11 12:49:02,084 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Psf preloaded
2024-01-11 12:49:02,084 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.RMS preloaded
2024-01-11 12:49:02,084 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.RMS.RMSField preloaded
2024-01-11 12:49:02,084 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.RMS.RMSFieldMap preloaded
2024-01-11 12:49:02,085 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.RayTracing preloaded
2024-01-11 12:49:02,085 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Spot preloaded
2024-01-11 12:49:02,085 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Surface preloaded
2024-01-11 12:49:02,085 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Settings.Wavefront preloaded
2024-01-11 12:49:02,085 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Tolerancing preloaded
2024-01-11 12:49:02,085 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Analysis.Tolerancing.QuickYield preloaded
2024-01-11 12:49:02,085 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Common preloaded
2024-01-11 12:49:02,085 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Editors preloaded
2024-01-11 12:49:02,085 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Editors.LDE preloaded
2024-01-11 12:49:02,085 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Editors.MCE preloaded
2024-01-11 12:49:02,085 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Editors.MFE preloaded
2024-01-11 12:49:02,086 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Editors.NCE preloaded
2024-01-11 12:49:02,086 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Editors.TDE preloaded
2024-01-11 12:49:02,086 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Preferences preloaded
2024-01-11 12:49:02,086 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.SystemData preloaded
2024-01-11 12:49:02,086 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools preloaded
2024-01-11 12:49:02,086 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools.FileManager preloaded
2024-01-11 12:49:02,086 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools.General preloaded
2024-01-11 12:49:02,086 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools.LMx preloaded
2024-01-11 12:49:02,086 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools.Optimization preloaded
2024-01-11 12:49:02,086 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools.RayTrace preloaded
2024-01-11 12:49:02,086 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Tools.Tolerancing preloaded
2024-01-11 12:49:02,087 - zospy.api.apisupport - DEBUG - Nested namespace ZOSAPI.Wizards preloaded
2024-01-11 12:49:02,244 - zospy.zpcore - DEBUG - Assigning ZOSAPI_Connection() to self.Connection
2024-01-11 12:49:02,565 - zospy.zpcore - DEBUG - ZOSAPI_Connection() already assigned self.Connection
2024-01-11 12:49:02,676 - zospy.zpcore - DEBUG - Closing connections with Zemax OpticStudio
ZOS instances: set()
@Eason152104
Copy link

Where can I download the development version or when will it be released? Looking forward to your definite answer; perplexed by this question "Cannot have more than one active ZOS instance"

@crnh
Copy link
Collaborator Author

crnh commented Jan 29, 2024

Hi @Eason152104,

The development version mentioned in this issue has been released as version 1.2.0. The comment was intended to prevent confusion due to the new connect method, which was not yet available when I opened this issue. As this version has now been released, I removed these comments to prevent further confusion.

Your comment suggests to me that you are actually looking for something else. Can you please indicate what you were trying to achieve, if / how that failed, and (if applicable) what behavior you expect from ZOSPy that will make it work?
If your issue is related to #19, please comment there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants