2

我正在使用botoPython 来自动化我的一些 EC2 工作流程。

这个问题很奇怪 - 脚本似乎冻结了一个简单变量的分配,但它在后台继续。最终,脚本将所有内容打印出来。

当我在 iPython 中逐行重复脚本时,没有问题,也没有冻结或等待(超出您在谈论 AWS 时的预期)。当我将它作为 Python 脚本运行时,输出似乎只是冻结了,直到脚本完成。

剧本:

def deploy_web_db_ami(a_key, a_pri, db_vol_id, db_vol_zone, db_vol_mnt):
    '''deploys a fresh instance for using as the web / db server.  
    '''

    #Connect to the EC2
    print "Connecting to AWS"
    conn = EC2Connection(a_key, a_pri)

    # get a ref to the image we want to install
    print "Searching for desired AMI image"
    image = conn.get_all_images(image_ids='ami-fd589594')


    print "Spinning up instance. . ."
    # Launch the image using our desired settings.
    reservation = image[0].run(key_name='my_kp', 
                               security_groups=['ssh'],
                               instance_type='t1.micro)
    print("we get this far before output freezes")
    ins = reservation.instances[0]


    print "Waiting for instance to move from pending to running. ",
    while (ins.state.lower() == u'pending'):
        print ". ",
        ins.update()
        time.sleep(5)

    time.sleep(5) # in case ssh is not up yet
    print "Instance %s's state changed to: %s" (ins.id, ins.state)
    if ins.state.lower() == u'running':
        # instance is up and running
        # Attach the db EBS volume
        print "Attaching DB EBS volume."
        conn.attach_volume(db_vol_id, ins.id, db_vol_mnt)
        p_dns = ins.dns_name
    else:
        print "ERROR - INSTANCE NOT RUNNING.:: %s" % ins.state

    print "All done!"
    return (ins.id, p_dns)

def total_web_deploy():
    deploy_web_db_ami('xxx', 'xxx', 'the id', 'the zone', '/mnt/sdf')
    some_other_functions(). .. 

我正在使用从命令行运行脚本fab total_web_deploy

输出将如下所示:

Connecting to AWS
Search for desired AMI image
Spinning up instance. . .
we get this far before output freezes

然后我们将不得不等待实例和所有内容在脚本的其余部分打印出来之前完成。不过,它显然一直在后台工作。

Waiting for instance to move from pending to running.  .  .  .  .  .  .  .  .  .  .  Instance i-95c389f6's state changed to: running
Attaching DB EBS volume.
All done!

有任何想法吗?

编辑我已经澄清了这个问题。

4

1 回答 1

2

也许您的输出正在被缓冲。

尝试直接写入 stderr 以排除这种情况:

import sys
sys.stderr.write("Equivalent message here\n")

或者:

import sys
sys.stdout.write("Equivalent message here\n")
sys.stdout.flush()
于 2011-11-29T13:22:32.537 回答