GithubHelp home page GithubHelp logo

Comments (12)

crnh avatar crnh commented on September 28, 2024 2

I tried
del zos
without success. Thanks for your help!

Okay, but this should work as well. It might be that the _instances attribute is not reset when deleting the zos object. I'll have a look into that.

Meanwhile, it may be useful to know that you can safely reuse an existing ZOS instance. If you want to reconnect with OpticStudio (and the previous connection has been closed), you can just call connect_as_extension or create_new_application on the existing zos object.

from zospy.

crnh avatar crnh commented on September 28, 2024 1

This is a good question, but not related to this issue, so it's better to create a new discussion for this. Thanks!

from zospy.

Omnistic avatar Omnistic commented on September 28, 2024 1

Hi @crnh,

Using your message I managed to understand my mistake. The problem was that upon calling

del zos

The Interactive Extension remains connected in OpticStudio (the Status window displays: Connected). If, after calling

del zos

I manually Terminate the interactive extension window in OpticStudio and open a new one. Then it seems to be fine. Sorry for the trouble, I guess I expected again a behaviour like the boilerplate code that OpticStudio generates. Do you think there could be a way to avoid having to manually Terminate the interactive extension in OpticStudio? Thanks for your help.

from zospy.

crnh avatar crnh commented on September 28, 2024

Hi @Omnistic, thanks for notifying us about this problem. This exception is raised from zpcore.py#L289, when creating an instance of the ZOS class. It should therefore occur only if a second instance of the ZOS class is instantiated.

Can you debug this code to confirm that the exception is raised as soon as you run this line?

zos = zp.ZOS()

This problem is quite weird and probably not related to the connection with OpticStudio, as this exception should already be raised before creating the connection. It might be related to other parts of your setup. Which development environment do you use?

from zospy.

Omnistic avatar Omnistic commented on September 28, 2024

I turned-off my computer to go to lunch, and tried the following when I returned to work

import zospy as zp

zos = zp.ZOS()

This time it worked once. But subsequent call to that snippet would produce the error. Even if I close OpticStudio entirely. I will try to restart my computer again to see if it makes a difference.

from zospy.

Omnistic avatar Omnistic commented on September 28, 2024

I'm having difficulty understanding what's happening. It seems that when I do restart my computer I can get it to work once, but then get the

ValueError: Cannot have more than one active ZOS instance

Even though with the OS template I can still connect. Could it have to do with how Ansys manage OS licence as oppose to Zemax previously? Its kind of hard to debug cause I need to restart my computer everytime.

from zospy.

crnh avatar crnh commented on September 28, 2024

It is actually correct that the second call to that snippet produces the error. Only one instance of the ZOS class can exist at the same time; if you try to create a second instance, it will check if there is already an existing instance and raise that error. The reason for this limit is that the ZOS-API doesn't support multiple connections from the same process. Since a ZOS object represents a connection to the ZOS-API (at least after calling ZOS.wakeup), we decided to allow only a single instance of this class.

You can check if there is already an existing instance of the ZOS class in various ways. e.g. running

zp.ZOS._instances

If this returns an empty set, there are no instances; if it returns a set with a single element, there is already an instance and any subsequent calls to zp.ZOS() will raise an error.

Another way to check for instances:

import gc

[o for o in gc.get_objects() if isinstance(o, zp.ZOS)]

By the way, which development environment are you using? If you are using Spyder, Jupyter or anything else running an ipython kernel, a ZOS instance will live as long as you don't manually delete it and don't stop the kernel. Restarting the kernel or deleting the zos object should be enough to clear it, and rebooting shouldn't be necessary then.

from zospy.

Omnistic avatar Omnistic commented on September 28, 2024

I'm using JupyterLab 3.3.2 and restarting the Kernel works! Sorry I didn't think about that, with the boilerplate code from OpticStudio this is not necessary. Is there any other way other than restarting the Kernel? I tried

del zos

without success. Thanks for your help!

from zospy.

jwmbeenakker avatar jwmbeenakker commented on September 28, 2024

Nice that has been sorted out.
@crnh Would it be an idea to add a frequently asked questions document with common problems and solutions? Could probably help others.

from zospy.

andibarg avatar andibarg commented on September 28, 2024

Hi all,

On a related note, I was wondering why as many as four lines are needed to initialize ZOSpy. Could zos.wakeup() not be called automatically when creating the instance via zos = zp.ZOS()? I think everyone wants to do that anyways, right? Same goes for zos.connect_as_extension() and zos.create_new_application() - why can they not return the primary systemoss right away?

If there is an opportunity to turn four lines of code into two, I would take it :). But sorry to sidetrack and in case I am being ignorant.

from zospy.

crnh avatar crnh commented on September 28, 2024

Hi @Omnistic, I looked into the problem with deleting the zos object and I'm unable to reproduce it. Can you run this in the REPL (so not in a jupyter notebook):

import zospy as zp
print(zp.ZOS._instances)
zos = zp.ZOS()
print(zp.ZOS._instances)
del zos
print(zp.ZOS._instances)

The first and last print statement should return an empty set, the second a set with a single element.
Deleting objects in Python actually only removes the reference to the object; the object itself will live on happily until the garbage collector removes it. This often happens immediately, but this seems not to be the case in your setup. Cleaning up the _instances attribute of the ZOS class is also triggered by the garbage collector. It is to the best of my knowledge not possible to do this immediately on running del zos; this is a technical limitation within Python.

from zospy.

crnh avatar crnh commented on September 28, 2024

Ah, that makes sense, thanks for checking this! As far as I know, it is possible to close the extension / standalone OpticStudio instance by calling zos.Application.CloseApplication(). We are considering to add this to the finalizer, so that this will be done automatically when calling del, but we first need to test if this works as expected.

Regarding the OpticStudio boilerplate code: it completely ignores that you can't create a second connection, and if you try to do so, it will happily return a new reference to the existing connection without telling you it's not a new connection. If you treat this connection as a new connection, you may run into problems, and that's why @LucVV decided to implement the ZOS class in this way. We are going to update the error message to explain this more clearly.

from zospy.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.