<div dir="ltr"><div>Ah. Thanks.. I removed the previous code. Please excuse me. I will rewrite the question so it is clear.<br><br></div><div>Here is my current solution in an imperative way. My application will work with AWS boto library to create EC2 instances and RDS instances. Assuming my API will simply send the request, and return if the request is accepted, I need to lock until the instance is ready before I can start the next operation. <br><br><pre>def create_vm(.....):
    # returns vm object

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

def get_vm_status(.....):
    return status


# NOTE: This is obviously a simplified version, but pretty much half of the code.
def lock_until_ready(conn, get_status_func, _id, max_wait=60):
    wait_count = 1
    status = get_status_func(conn, _id)
    while status != 'available' and wait_count < max_wait:
        print("Querying status (attempt {i}): {status}".format(
            i=wait_count, status=status))
        wait_count += 1
        time.sleep(10)
        status = get_status_func(conn, _id)


def clone(data_center, image_id, options):

    conn = get_connection(data_center)
    vm_id = create_vm(conn, image_id, ....)
    lock_until_ready(conn, get_vm_status, vm_id, max_wait=30)
    modify_vm(conn, options)
    lock_until_ready(conn, get_vm_status, vm_id, max_wait=30)

</pre></div><div><font face="arial,helvetica,sans-serif">I hope this doesn't come across as a code review. This works. I made my lock function extensible and testable, but I feel like there should be a better more user-friendly way, even in the imperative world in Python. I thought of context manager because I can do some clean up on entry (verify the db name has not been taken), and exit (if fail rollback). If you are familiar with cloudformation, it almost seems like that's what I am doing. I am writing this because I have custom needs that cloudformation can't do elegantly without many hops. API is much more flexible for my current task, FYI.<br><br></font></div><div><font face="arial,helvetica,sans-serif">Any feedback is welcome. Thank you.<br><br></font></div><div><font face="arial,helvetica,sans-serif">John<br></font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 16, 2015 at 10:53 AM, Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Thu, Sep 17, 2015 at 12:34 AM, John Wong <<a href="mailto:gokoproject@gmail.com">gokoproject@gmail.com</a>> wrote:<br>
> Sorry first time posting to tutor / general list. Usually on TIP list. As<br>
> per Mark's recommendation, now posting to <a href="mailto:python-list@python.org">python-list@python.org</a>.<br>
<br>
</span>But, sadly, without a lot of context. When you change lists, it's<br>
helpful to include a lot of extra verbiage so folks who don't follow<br>
the other list can pick up where you were.<br>
<br>
ChrisA<br>
<span class="HOEnZb"><font color="#888888">--<br>
<a href="https://mail.python.org/mailman/listinfo/python-list" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br></div>