jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.72k stars 4.96k forks source link

Is it possible to limit the number of running notebooks in jupyter? #615

Open jlamcanopy opened 9 years ago

jlamcanopy commented 9 years ago

I want to restrict users from starting too many notebooks. Is it possible? Thank you!

minrk commented 9 years ago

Not super easily. You could use a custom KernelManager that refuses to start new kernels if more than a certain number are already open. I don't think you will have a particularly good experience with error messages if you do that, though.

jlamcanopy commented 9 years ago

@minrk I'm new to Jupyter or Ipython but how do you control how much memory and CPU a user can use? For instance, a mad scientist can create thousand of notebooks in jupyter :)

jdfreder commented 9 years ago

@jlamcanopy you can use JupyterHub + your operating system's features for managing user accounts to limit the amount of memory per user. JupyterHub will simply make sure the server process is launched using the user's token.

https://github.com/jupyter/jupyterhub

jlamcanopy commented 9 years ago

@jdfreder Thanks for confirming this and your advice on how it could be done. Do you think it is useful to have a feature to have a configuration for limiting the number of notebooks running in jupyter?

jdfreder commented 9 years ago

I suppose in conjuncture with something like JupyterHub, it could be useful. But I do think if you are trying to control memory and cpu usage, it's best to do that directly. I'll mark this as wish list, and others can weigh in - or someone can come along and implement it ;)

minrk commented 9 years ago

I think this is probably one of those things like custom auth that probably shouldn't be implemented so much in the single-user notebook, but some code could be added to the single-user notebook to make it easier for managed deployments like JupyterHub to configure. We might already be at that point, though—registering a custom KernelManager that refuses to start new kernels is already available via existing config. But I'm pretty sure that failing to start kernels is not an error we handle well.

SherryNJU commented 7 years ago

Hi, @jlamcanopy Did you get process of implementing this requirement? We are in a need to implement this feature too, if any information you would like to share with us?

a3626a commented 2 years ago

It is possible now. (in jupyter server) In some cases, just shutdown existing kernel is better than refusing kernel start. (just ignore debug messages)

from jupyter_server.services.kernels.kernelmanager import AsyncMappingKernelManager
class CustomKernelManager(AsyncMappingKernelManager) :
    async def start_kernel(self, kernel_id=None, path=None, **kwargs):
        print("CustomKernelManager start_kernel")
        print(f"Num Kernels Running: {len(self.list_kernels())}")
        kernels = self.list_kernels()
        if len(kernels) > 2 :
            await self.shutdown_kernel(kernels[0]["id"])
        return await AsyncMappingKernelManager.start_kernel(self, kernel_id, path, **kwargs)
c.ServerApp.kernel_manager_class = CustomKernelManager
BwL1289 commented 7 months ago

For jupyterhub (not jupyter notebooks for which this question was asked), for anyone who finds this, you can use active_server_limit .