-
Notifications
You must be signed in to change notification settings - Fork 174
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
Add close method to Simulator. #859
Conversation
c519d98
to
b42a8b4
Compare
This does not add any global tracking of open simulators or automatic closing |
[Mega nit-pick!] Any reason why Also, briefly glancing at |
Yeah, I think there should be a specific exception subclass for this, similar to |
For now, I would go with |
Specifically |
Closed file object raise a
|
Which seems somewhat reasonbale considering that |
Yeah, that makes some sense I guess. Trying to write to a read-only array in Numpy used to give a RuntimeError, I think, but now gives a ValueError; it's a bit similar to our situation here. But in |
This may be a TODO for another PR, but just a pedagogical comment. Right now So it'd just be good if we could get users in the habit of gracefully closing their simulators as they learn Nengo, to make the transition between backends smoother. If we really wanted to encourage this, we could do something like def __del__(self):
if not self.closed:
warnings.warn("Simulator deleted without being closed") That might be overkill, we could also just switch to using the context manager syntax in our examples, so that new users just learn that and don't question it. Or if we wanted to be more magicy, we could call |
I wasn't thinking about putting the call in |
Hmm.. I don't think the added complexity (from the perspective of a user) is worth it. Having all those sim.close() lines cluttering up the code feels wrong to me. I'd much rather go with what @hunse mentioned about having The main reason that having the if isinstance(sim, nengo_spinnaker.Simulator):
sim.close() or if hasattr(sim, 'close'):
sim.close() or try:
sim.close()
except AttributeError:
pass Having a |
I don't think there's any way around forcing people to call Only nitpick is that we should do this with |
Yeah, and when we keep in mind the context manager syntax, I don't think we'll have a bunch of with nengo.Simulator(net) as sim:
sim.run(1) instead of sim = nengo.Simulator(net)
sim.run(1) The nice thing about for i in range(10):
sim = nengo.Simulator(net[i])
sim.run(1) (i.e. it handles the simulator getting deleted mid-script). But I'll admit that I've never used |
Right, I see what you're saying Dan. So it should probably be both; have the check in the |
Hmm... I think I see what you're saying about consistency... my problem might be is that I'm mentally expecting it to behave like how file handles in Python behave -- no one forces me to manually call So I was expecting this to behave in the same way. Are there other examples of things in Python where you have to call |
Why can't we have both? It will be done automatically at exit or
This is exactly what I had in mind. Is there anyone making an argument against this? It seems to me we're all pretty much on the same page here, and I'm not quite sure what we're actually discussing. |
😱
👍 |
This isn't really a situation where we should look to Python. Python doesn't do any validation of function inputs either, but we dedicate a lot of our codebase to validation in order to make Nengo easier for our users. I'm envisioning a |
What is the problem with automatically closing simulators? I just don't see why we want to encourage manual closing. Certainly some backends might require it if you want to make more than one simulator (that's why this issue came up in the first place), but that's something those backends can deal with (they could even throw an exception if a second simulator is made once one already exists, or something). |
I think it's based on the reliability of |
I find it hard to dissociate the discussion of manually closing with having a Sidenote: whoo, Nengo discussions! It's been too long 🐙 🌈 |
Fair enough. I would do it context manager style then, and have |
Hmm, it feels to me like we're veering a lot from the original purpose here. The intent of the PR was to add a dummy close() method so that people wouldn't have to do |
Agreed! :) :) :) |
When you're doing backend-specific things, yes, you need to know about your backend. So, for example, nengo_spinnaker has a limitation that only one Simulator can be open at a time. So if I try doing more than one, I get an error saying "hey, you should close the old one". Right now, that requires doing one of these The intent here was to get rid of that bit of annoying code, and, since it's likely that some other backends might have a similar |
But creating a best-practice that users always call I do agree, though, that I think we can merge this PR as-is, and push that discussion until later. I think this is an improvement no matter what direction we take down the road. |
I completely agree with you. This PR was not meant to introduce such a best-practice.
Yup, I'd definitely be interested in that sort of discussion, long-term (in amongst all the other design discussions -- yay!). |
That reminds me, the other reason this discussion came up is because we were talking about standardizing that |
Anyway, in the course of this discussion, I looked over the PR, and it looks good to me. |
Yeah I didn't take any of this as blocking this PR, this all started with "This may be a TODO for another PR...". I can make a separate issue if you want to keep the discussion out of this thread. |
Adresses #739, #857.