#๐Ÿ”’ Advice for use of Singletons

9 messages ยท Page 1 of 1 (latest)

bleak abyss
#

Hey everyone,
Looking for some advice on whether this situation is appropriate for use of a Singleton, or whether it would be expected behaviour.

Context:
I am building an object-oriented wrapper for the VirtualBox SOAP API. It has an interface to the SOAP API, which binds all methods of the service to the interface object, allowing the methods to be called in an object-oriented way.
On top of this, there are model classes for a given 'category' within the API, which make use of a subset of methods of the interface. These models define methods to get/set properties using the specific interface, to give an object-oriented appearance; all interface methods are handled out-of-sight.

For some models, like the model representing a virtual machine, there exists a session, where the machine may be locked - i.e. mutable. All models have a unique handle assigned by the VirtualBox API.
While a locked model for the same virtual machine will have the same ID as an unlocked model, the handles will be different - one will reference the mutable object on the remote API, the other the immutable object, though both are still valid objects, until the mutable object is unlocked.

Question:
Would it be sensible to return the same instance of the model if the handles match, meaning the associated session attributes would be the same, etc.
For example:

machine = api.get_machine()
locked_machine = machine.lock()
locked_machine.name = "New name"

assert machine is api.get_machine()
assert machine.session is api.get_machine().session
assert locked_machine is api.get_machine().lock()

As I understand, storing all instantiated models in a dict of a metaclass, etc. means that they will not be garbage collected, as they will still be referenced, which is poor memory management, but the consistency of getting the same model with the current session state each time seems like a plus. Can someone sanity check me and let me know what they would expect from something like this?

dry sageBOT
#

@bleak abyss

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

bleak abyss
#

Please let me know if you need any more information or context.
Thanks :)

peak eagle
bleak abyss
#

Ooh that's exciting, sounds like a perfect way to store instances while they're in use. Thank you very much!

#

I will have a play with those :)

#

!close

dry sageBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.