modal.queue
modal.queue.Queue
class Queue(modal.object.Provider)
A distributed, FIFO Queue available to Modal apps.
The queue can contain any object serializable by cloudpickle
.
def __init__(self):
persist
@typechecked
def persist(self, label: str, namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE):
Deploy a Modal app containing this object. This object can then be imported from other apps using
the returned reference, or by calling modal.SharedVolume.from_name(label)
(or the equivalent method
on respective class).
Example Usage
import modal
volume = modal.SharedVolume().persist("my-volume")
stub = modal.Stub()
# Volume refers to the same object, even across instances of `stub`.
@stub.function(shared_volumes={"/vol": volume})
def f():
pass
from_name
@classmethod
def from_name(
cls: Type[P],
app_name: str,
tag: Optional[str] = None,
namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
) -> P:
Returns a reference to an Modal object of any type
Useful for referring to already created/deployed objects, e.g., Secrets
import modal
stub = modal.Stub()
@stub.function(secret=modal.Secret.from_name("my-secret-name"))
def some_function():
pass
lookup
@classmethod
def lookup(
cls: Type[P],
app_name: str,
tag: Optional[str] = None,
namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
client: Optional[_Client] = None,
) -> H:
General purpose method to retrieve Modal objects such as functions, shared volumes, and secrets.
import modal
square = modal.Function.lookup("my-shared-app", "square")
assert square(3) == 9
vol = modal.SharedVolume.lookup("my-shared-volume")
for chunk in vol.read_file("my_db_dump.csv"):
...
modal.queue.QueueHandle
class QueueHandle(modal.object.Handle)
Handle for interacting with the contents of a Queue
stub.some_dict = modal.Queue()
if __name__ == "__main__":
with stub.run() as app:
app.some_dict.put({"some": "object"})
def __init__(self):
is_hydrated
def is_hydrated(self) -> bool:
# A hydrated Handle is fully functional and linked to a live object in an app
# To hydrate Handles, run an app using stub.run() or look up the object from a running app using <HandleClass>.lookup()
from_id
@classmethod
def from_id(cls: Type[H], object_id: str, client: Optional[_Client] = None) -> H:
Get an object of this type from a unique object id (retrieved from obj.object_id
)
object_id
@property
def object_id(self) -> str:
A unique object id for this instance. Can be used to retrieve the object using .from_id()
from_app
@classmethod
def from_app(
cls: Type[H],
app_name: str,
tag: Optional[str] = None,
namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
client: Optional[_Client] = None,
) -> H:
Returns a handle to a tagged object in a deployment on Modal.
get
def get(self, block: bool = True, timeout: Optional[float] = None) -> Optional[Any]:
Remove and return the next object in the queue.
If block
is True
(the default) and the queue is empty, get
will wait indefinitely for
an object, or until timeout
if a timeout
is specified. Raises the native Python queue.Empty
exception if the timeout
is reached.
If block
is False
, get
returns None
immediately if the queue is empty. The timeout
is
ignored in this case.
get_many
def get_many(self, n_values: int, block: bool = True, timeout: Optional[float] = None) -> List[Any]:
Remove and return up to n_values
objects from the queue.
If block
is True
(the default) and the queue is empty, get
will wait indefinitely for
the next object, or until timeout
if a timeout
is specified. Raises the native Python queue.Empty
exception if the timeout
is reached. Returns as many objects as are available (less then n_values
)
as soon as the queue becomes non-empty.
If block
is False
, get
returns None
immediately if the queue is empty. The timeout
is
ignored in this case.
put
def put(self, v: Any) -> None:
Add an object to the end of the queue.
put_many
def put_many(self, vs: List[Any]) -> None:
Add several objects to the end of the queue.