Skip to content

stop_cluster

Stops a cluster, does not terminate it. If the cluster had been saved to json, can use get_cluster_from_json to restart the cluster.

Parameters:

Name Type Description Default
quokka_cluster EC2Cluster

Cluster to stop.

required
Return

None

Examples:

>>> from pyquokka.utils import *
>>> manager = QuokkaClusterManager()
>>> cluster = manager.create_cluster(aws_access_key, aws_access_id, num_instances = 2, instance_type = "i3.2xlarge", ami="ami-0530ca8899fac469f", requirements = ["numpy", "pandas"])
>>> cluster.to_json("my_cluster.json")
>>> manager.stop_cluster(cluster)
Source code in pyquokka/utils.py
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
def stop_cluster(cluster_json):

    """
    Stops a cluster, does not terminate it. If the cluster had been saved to json, can use `get_cluster_from_json` to restart the cluster.

    Args:
        quokka_cluster (EC2Cluster): Cluster to stop.

    Return:
        None

    Examples:

        >>> from pyquokka.utils import *
        >>> manager = QuokkaClusterManager()
        >>> cluster = manager.create_cluster(aws_access_key, aws_access_id, num_instances = 2, instance_type = "i3.2xlarge", ami="ami-0530ca8899fac469f", requirements = ["numpy", "pandas"])
        >>> cluster.to_json("my_cluster.json")
        >>> manager.stop_cluster(cluster)

    """

    ec2 = boto3.client("ec2")
    with open(cluster_json, "r") as f:
        cluster_info = json.load(f)
    instance_ids = list(cluster_info['instance_ids'].values())
    ec2.stop_instances(InstanceIds = instance_ids)
    while True:
        time.sleep(0.1)
        a = ec2.describe_instances(InstanceIds = instance_ids)
        states = [a['Reservations'][0]['Instances'][i]['State']['Name'] for i in range(len(instance_ids))]
        if "running" in states:
            continue
        else:
            break