我的团队正试图通过关闭Jupyterlab内部的所有内核和开放终端来削减我们的云成本预算.这可以通过导航到打开的内核部分,然后为内核和终端单击Shut Down All来轻松完成.在此过程中,其他用户正在工作,他们只需重新启动其内核即可.我们的 idea 是让一个脚本在每个人都停止工作的某个时刻关闭所有内核和终端.

话虽如此,但在命令行中关闭单个jupyterlab内核时,我遇到了一些困难.Is it possible to shut down a jupyter kernel through command line?我想要类似于以下的东西:

jupyter --change_kernel None file.ipynb

try

  1. kill -9 {ipynb_pid}.
    Killing a jupyterlab pid does not shut down the kernel. Jupyter thinks this is an error, and subsequently restarts it in another pid.

  2. modify the metadata to have No kernel.
    As per this post, I have tried to change the kernel by editing the json of the ipynb files that I want to kill. I.e. jq '.metadata.kernel="None"' my_file.ipynb > my_file.ipynb. This leads to very spicy overwrite issues. Also, if there are unsaved changes, this does not play nice.

推荐答案

以下是我为解决此问题而编写的PowerShell函数-

function DeleteJupyterKernel {
    param (
        [string]$baseUrl = "http://localhost:8888" ,    # Base URL of your Jupyter server
        [string]$token = "mytoken",      # Your Jupyter access token
        [string]$kernelId    # ID of the kernel to delete
    )
    
    # Create a web session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession

    # Initial GET request to set the XSRF token
    $getResponse = Invoke-WebRequest -Uri "$baseUrl/tree?" -WebSession $session -UseBasicParsing

    # Extract the XSRF token
    $xsrfToken = $session.Cookies.GetCookies($baseUrl)['_xsrf'].Value

    # Define headers for DELETE request
    $headers = @{
        "X-XSRFToken" = $xsrfToken
        "Authorization" = "token $token"
    }

    # DELETE request URL
    $deleteUri = "$baseUrl/api/kernels/$kernelId"

    # Perform the DELETE request
    $response = Invoke-RestMethod -Uri $deleteUri -Method Delete -Headers $headers -WebSession $session -SkipCertificateCheck

    return $response
}

然后,您可以通过将内核id传递给此函数来调用此函数,如下所示-

DeleteJupyterKernel -kernelId "<kernelid>"

你可以获取内核id,做类似的事情,见下文-

function listjupytersessions{

    # Define the URL for GET request
    $getUri = "http://localhost:8888/api/sessions/?token=mytoken"
    
    # Send the GET request
    $response = Invoke-RestMethod -Uri $getUri -Method Get -SkipCertificateCheck
    # Initialize the Counter
    $counter = 1
    # Process each session
    foreach ($session in $response) {
        $name = $session.name
        $kernelId = $session.kernel.id
        $kernelName = $session.kernel.name
        $executionState = $session.kernel.execution_state
    
        # Output the extracted information
        Write-Host ""
        Write-Host "------KernelInfo for Kernel $counter with Id $kernelId Starts------"
        Write-Host "Notebook Name: $name"
        Write-Host "Kernel ID: $kernelId"
        Write-Host "Kernel Name: $kernelName"
        Write-Host "Execution State: $executionState"
        Write-Host "------KernelInfo for Kernel $counter with Id $kernelId Ends------"
        $counter++
    }
    $counter = 1
}

让我知道进展如何.

Python-3.x相关问答推荐

具有多个值的极轴旋转和熔化/取消旋转(反转旋转)操作(Pandas 堆叠/取消堆叠交替/UDF覆盖)

PythonPandas 创建一个列并添加到DataFrame

visual studio代码窗口中未激活虚拟环境11

tkinter/python3.9 中的 Entry 子类和用户输入重复的问题

SQL Server 2022和Python3.10脚本错误

如何查找以开头并替换的字符串

我正在使用 python 线程,当查询 mysql 时,代码似乎在运行并保持在无限循环中,没有返回任何错误

为什么 mypy 不适用于 sqlalchemy?

Python多进程:运行一个类的多个实例,将所有子进程保留在内存中

Python defaultdict 在获取时返回 None,尽管使用默认值初始化

机器学习实验笔记本的工作区 url

Pytorch:图像标签

为什么 f-strings 比 str() 更快地解析值?

没有可重定向到的 URL.提供一个 url 或在模型上定义一个 get_absolute_url 方法

具有 2 个输入的 python 3 map/lambda 方法

smtplib 在 Python 3.1 中发送带有 unicode 字符的邮件的问题

Python configparser 不会接受没有值的键

类型提示返回 NameError: name 'datetime' not defined

变量类型注解NameError不一致

将文本写入 gzip 文件