커널 함수

class ai.backend.client.kernel.Kernel(kernel_id, owner_access_key=None)[소스]

연산 세션들을 관리하면서 다양한 상호작용을 제공합니다.

Backend.AI의 개발이 진행됨에 따라 '커널'이라는 용어는 '연산 세션'이라는 용어로 대체되었지만, 클라이언트 세션과의 혼동을 피하고 과거 코드와의 호환성을 위해 여전히 이 함수 클래스는 커널이라는 이름을 사용하고 있습니다.

여러 개의 다중 컨테이너로 이뤄진 연산 세션에서는, destroy()restart() 메소드를 제외한 모든 메소드는 세션의 마스터 컨테이너에게만 유효합니다. 따라서 여러 컨테이너가 동일한 데이터를 바라보거나 분산 처리하도록 하기 위해서는 가상폴더 탑재 옵션을 활용하여야 합니다. 현재 이러한 작업은 사용자의 몫입니다. (추후 업데이트로 편의성 개선 예정)

session = None

이 함수 클래스가 사용할 클라이언트 세션 인스턴스

classmethod await get_or_create(image, *, client_token=None, type_='interactive', enqueue_only=False, max_wait=0, no_reuse=False, mounts=None, envs=None, startup_command=None, resources=None, resource_opts=None, cluster_size=1, domain_name=None, group_name=None, tag=None, scaling_group=None, owner_access_key=None)[소스]

Get-or-creates a compute session. If client_token is None, it creates a new compute session as long as the server has enough resources and your API key has remaining quota. If client_token is a valid string and there is an existing compute session with the same token and the same image, then it returns the Kernel instance representing the existing session.

매개변수
  • image (str) -- The image name and tag for the compute session. Example: python:3.6-ubuntu. Check out the full list of available images in your server using (TODO: new API).

  • client_token (Optional[str]) -- A client-side identifier to seamlessly reuse the compute session already created.

  • type --

    Either "interactive" (default) or "batch".

    버전 19.09.0에 추가.

  • enqueue_only (bool) --

    Just enqueue the session creation request and return immediately, without waiting for its startup. (default: false to preserve the legacy behavior)

    버전 19.09.0에 추가.

  • max_wait (int) --

    The time to wait for session startup. If the cluster resource is being fully utilized, this waiting time can be arbitrarily long due to job queueing. If the timeout reaches, the returned status field becomes "TIMEOUT". Still in this case, the session may start in the future.

    버전 19.09.0에 추가.

  • no_reuse (bool) --

    Raises an explicit error if a session with the same image and the same client_token already exists instead of returning the information of it.

    버전 19.09.0에 추가.

  • mounts (Optional[Iterable[str]]) -- The list of vfolder names that belongs to the currrent API access key.

  • envs (Optional[Mapping[str, str]]) -- The environment variables which always bypasses the jail policy.

  • resources (Optional[Mapping[str, int]]) -- The resource specification. (TODO: details)

  • cluster_size (int) -- The number of containers in this compute session. Must be at least 1.

  • tag (Optional[str]) -- An optional string to annotate extra information.

  • owner -- An optional access key that owns the created session. (Only available to administrators)

반환 형식

Kernel

반환값

The Kernel instance.

await destroy(*, forced=False)[소스]

Destroys the compute session. Since the server literally kills the container(s), all ongoing executions are forcibly interrupted.

await restart()[소스]

Restarts the compute session. The server force-destroys the current running container(s), but keeps their temporary scratch directories intact.

await interrupt()[소스]

Tries to interrupt the current ongoing code execution. This may fail without any explicit errors depending on the code being executed.

await complete(code, opts=None)[소스]

Gets the auto-completion candidates from the given code string, as if a user has pressed the tab key just after the code in IDEs.

Depending on the language of the compute session, this feature may not be supported. Unsupported sessions returns an empty list.

매개변수
  • code (str) -- An (incomplete) code text.

  • opts (Optional[dict]) -- Additional information about the current cursor position, such as row, col, line and the remainder text.

반환 형식

Iterable[str]

반환값

An ordered list of strings.

await get_info()[소스]

Retrieves a brief information about the compute session.

await get_logs()[소스]

Retrieves the console log of the compute session container.

await execute(run_id=None, code=None, mode='query', opts=None)[소스]

Executes a code snippet directly in the compute session or sends a set of build/clean/execute commands to the compute session.

For more details about using this API, please refer the official API documentation.

매개변수
  • run_id (Optional[str]) -- A unique identifier for a particular run loop. In the first call, it may be None so that the server auto-assigns one. Subsequent calls must use the returned runId value to request continuation or to send user inputs.

  • code (Optional[str]) -- A code snippet as string. In the continuation requests, it must be an empty string. When sending user inputs, this is where the user input string is stored.

  • mode (str) -- A constant string which is one of "query", "batch", "continue", and "user-input".

  • opts (Optional[dict]) -- A dict for specifying additional options. Mainly used in the batch mode to specify build/clean/execution commands. See the API object reference for details.

반환값

An execution result object

await upload(files, basedir=None, show_progress=False)[소스]

Uploads the given list of files to the compute session. You may refer them in the batch-mode execution or from the code executed in the server afterwards.

매개변수
  • files (Sequence[Union[str, Path]]) --

    The list of file paths in the client-side. If the paths include directories, the location of them in the compute session is calculated from the relative path to basedir and all intermediate parent directories are automatically created if not exists.

    For example, if a file path is /home/user/test/data.txt (or test/data.txt) where basedir is /home/user (or the current working directory is /home/user), the uploaded file is located at /home/work/test/data.txt in the compute session container.

  • basedir (Union[str, Path, None]) -- The directory prefix where the files reside. The default value is the current working directory.

  • show_progress (bool) -- Displays a progress bar during uploads.

await download(files, dest='.', show_progress=False)[소스]

Downloads the given list of files from the compute session.

매개변수
  • files (Sequence[Union[str, Path]]) -- The list of file paths in the compute session. If they are relative paths, the path is calculated from /home/work in the compute session container.

  • dest (Union[str, Path]) -- The destination directory in the client-side.

  • show_progress (bool) -- Displays a progress bar during downloads.

await list_files(path='.')[소스]

Gets the list of files in the given path inside the compute session container.

매개변수

path (Union[str, Path]) -- The directory path in the compute session.

stream_events()[소스]

Opens the stream of the kernel lifecycle events. Only the master kernel of each session is monitored.

반환 형식

SSEResponse

반환값

a StreamEvents object.

stream_pty()[소스]

Opens a pseudo-terminal of the kernel (if supported) streamed via websockets.

반환 형식

StreamPty

반환값

a StreamPty object.

stream_execute(code='', *, mode='query', opts=None)[소스]

Executes a code snippet in the streaming mode. Since the returned websocket represents a run loop, there is no need to specify run_id explicitly.

반환 형식

WebSocketResponse

class ai.backend.client.kernel.StreamPty(session, underlying_ws)[소스]

A derivative class of WebSocketResponse which provides additional functions to control the terminal.