spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.25k stars 1.6k forks source link

Restart kernel before run a code #6955

Open helderc opened 6 years ago

helderc commented 6 years ago

Would be great to have a option to restart the kernel every time you run a source file. By doing this it forces the iPython to reload all libs and free allocated memory.

ccordoba12 commented 6 years ago

Restarting a kernel is (usually) a very expensive operation, i.e. it takes a lot of time and (I don't understand exactly why) it blocks the rest of interface. That's why we don't have plans to support it on every run.

But you can use the option called Remove all variables before execution, present in

Run > Configuration per file

or

Preferences > Run

which clears everything in your kernel on each run, but doesn't require a restart.

CAM-Gerlach commented 6 years ago

By doing this it forces the iPython to reload all libs

Also, Spyder's UMR (User Module Reloader) feature, enabled by default, will also reload any local modules/packages (i.e. those that are likely to change).

helderc commented 6 years ago

I see but, seen the use of GPU memory this wont to free all used space.

CAM-Gerlach commented 6 years ago

Fair enough, but if the restart process alone takes 5-10 seconds and occupies the UI, much less running what must be an intensive script, then saving the fraction of a second needed to tap Ctrl - . Enter beforehand to trigger isn't too meaningful, I would think.

Also, you can just set your file under run configuration (or your master run config for all files) to always run in a dedicated console, so it'll launch a new one every time you run it, and you can easily Ctrl - W the old one whenever you're done with it, which closes essentially instantly vs. the long, agonizing and sometimes failure-prone process of a restart, and is thus actually faster than restarting the existing kernel, especially in terms of user-occupied time.

street-grease-coder commented 5 years ago

any practical solution to this?

CAM-Gerlach commented 5 years ago

@street-grease-coder None of the options suggested above work for your usecase?

If not, can you explain the specifics of what these don't address with your use case, so we can better understand your problem? Thanks.

suzannejb commented 4 years ago

My own problem is that I have to restart the kernel after every execution. What can I do to avoid this, or, the automatic restart would be useful.

ccordoba12 commented 4 years ago

@suzannejb, why do you need to do that?

suzannejb commented 4 years ago

I will have to get a bit of code to show, but basically, the script runs a first time, then the second time it errors out. I then have to restart kernel before I can get the script to run successfully.

CAM-Gerlach commented 4 years ago

@suzannejb Hey, have you tried the option Remove all variables before execution under Run > Configuration per file? This clears the console namespace and is equivalent to restarting the console but much faster, except for that it doesn't re-import modules unless they are changed (which shouldn't be an issue aside from incorrectly written or otherwise very tricky module code), and there is a slight possibility of other hidden/cached state somewhere that is not cleared (but again, very unlikely and not something code should be relying on).

Also, could you consider the other options I've discussed above? Do none of them work for your usecase? Thanks!

bokey007 commented 2 years ago

I have similar needs but for different use case.

I want to repeat same stochastic experiment several time.

Before starting each experiment I need to make sure all the variables in the workspace are deleted and the RAM is cleared. I also need to make use that the gpu memory is cleared.

I have kept all the experiment in a for loop

ccordoba12 commented 2 years ago

Ok, I think the work @impact27 is doing in PR #16914 will make this possible because it reduces the time to start a kernel a lot.

So we'll try to take a look at this in the coming months.

bokey007 commented 2 years ago

I don't mind if kernel restart takes times. Since in my case one experiment takes hours to complete and I have to automatically run 100s of such experiment, one after anather.

I just need to clear workspace, Ram and GPU memory from the python program itself, since I am looping over experiments in a for a loop.

And I assume kernel restart will not serve the purpose in my particular use case.

Will you be kind enough to suggest what could be done and how ?

CAM-Gerlach commented 2 years ago

I just need to clear workspace, Ram and GPU memory from the python program itself, since I am looping over experiments in a for a loop.

In that case, having GUI functionality from within Spyder won't really help you, and you won't really be able to do so inside the kernel itself while retaining control of the new one. While I suggest instead is using of the many techniques (multiprocessing library, joblib, subinterpreters, subprocess, etc, or even a bash script) to run your code from another separate, main process, which doesn't really intersect with the IDE used.

ccordoba12 commented 2 years ago

Good point @CAM-Gerlach! @bokey007, you can also open as many consoles as you want in Spyder and run your experiments in parallel in each one of them.

CAM-Gerlach commented 2 years ago

Yeah, though for hundreds of runs, its probably easier for the user to just handle that programmatically by launching threads, processes, subprocessies, subinterpreters, etc rather than trying to do so manually within Spyder (at least without leveraging the plugin API or something else)

rreilink commented 2 years ago

Another use case: I am using some scripts that spawn threads and connect to a TCP/IP server which accept only one connection at a time (since it is bound to an external hardware setup). Just clearing the variables does not close the socket connection of the previous execution of the script and thus causes subsequent invokations to fail. Nice to see it's planned for 5.3.3 👍

impact27 commented 1 year ago

One option to do this in code is to just add to the end of your script:

import ctypes; ctypes.string_at(0)

This will create a segmentation fault (except on windows) and restart the kernel.