[Tutor] Is context manager the answer to synchronous function calls?

John Wong gokoproject at gmail.com
Wed Sep 16 02:46:31 CEST 2015


Hi

Here is the code:

def create_vm(.....):
    # returns vm object

def modify_vm(.....):
    return new vm object

def get_vm_status(.....):
    return status
# Constraints:
# I cannot modify vm until vm is finished,
# I also cannot modify VM while the VM is being updated.
# Obviously it looks ugly and wrong to include the while
# in my functio. Those are user APIs.

# Straight-forward option
create_vm(...)
status = get_vm_status(...)
while status != "ready":
    time.sleep(10)
    status = get_vm_status(...)
modify_vm(....)
while status != "ready":
    time.sleep(10)
    status = get_vm_status(...)
print("done!")


I was thinking about context manager. But I feel like that's not the
right choice. If you can guess cloning a vm in my program will require
several more function calls. What's your recommendation to accomplish
this?

Thank you.

John


More information about the Tutor mailing list