floq.client.worker

This module provides interface for managing remote TPU worker.

class floq.client.worker.WorkerManager(client: floq.client.api_client.ApiClient, handler: floq.client.sse.EventStreamHandler)

Bases: object

Manages remote TPU worker.

The Floq service does not handle worker commands immediately, but schedules the request for execution and starts emitting events along with run progress. Thus, when calling start, stop or restart method the manager sends the request to the Floq service and opens an event stream connection. Every time a new message is received, the on_worker_command_event() callback method is called and prints an output message once the execution is done.

All TPU worker commands are executed synchronously by default. It means the further code execution will be blocked until the Floq service sends an event with floq.client.schemas.TaskState.DONE status. Optionally the request can be executed asynchronously (in a separate thread) by passing async_request=True argument to the corresponding methods.

class Command(value)

Bases: enum.IntEnum

TPU worker command.

RESTART = 1

Restarts TPU worker

START = 2

Starts TPU worker

STOP = 3

Stops TPU worker

on_worker_command_event(event: floq.client.schemas.TaskStatusEvent, context: Optional[floq.client.worker.WorkerManager.Command]) None

Callback function triggered after receiving a worker command progress event.

Parameters
  • event – Received event.

  • context – Optional user context data passed together with the event.

restart(async_request: bool = False) Optional[threading.Thread]

Restarts worker.

This method makes synchronous request to the Floq API service, thus it will block code execution until the TPU worker is up and running. To make it asynchronous, pass async_request=True.

Parameters

async_request – Indicates if the request should be executed asynchronously.

Returns

threading.Thread object handle if the async_request argument True, None otherwise.

start(async_request: bool = False) Optional[threading.Thread]

Starts worker.

This method makes synchronous request to the Floq API service, thus it will block code execution until the TPU worker is up and running. To make it asynchronous, pass async_request=True.

Parameters

async_request – Indicates if the request should be executed asynchronously.

Returns

threading.Thread object handle if the async_request argument True, None otherwise.

status() floq.client.schemas.Worker

Gets current worker status.

Returns

Worker object.

stop(async_request: bool = False) Optional[threading.Thread]

Stops worker.

This method makes synchronous request to the Floq API service, thus it will block code execution until the TPU worker is up and running. To make it asynchronous, pass async_request=True.

Parameters

async_request – Indicates if the request should be executed asynchronously.

Returns

threading.Thread object handle if the async_request argument True, None otherwise.