我开始着手一个新的Python 3.6项目,与AWS Boto通信。由于云服务器(EC2)需要一些时间来启动,因此我开始使用
我做错了什么?
asyncio
库,但是我对其有些困惑。
我希望异步地启动两个EC2实例。但是,如果我调用run_tests
,则会出现以下错误:
ERROR:asyncio:Task was destroyed but it is pending!
这是当前我的代码:
from manager import Manager
import asyncio
async def run_new_vm(manager, loop, vm_name):
new_instance = manager.launch_ec2_instance(vm_name)
task = loop.create_task(new_instance)
task.add_done_callback(lambda f: do_something(manager, f.result()))
def do_something(manager, instance):
// Do stuff once the instance is usable
async def one_service_per_vm(n, manager, loop):
for x in range (0, n):
print('Started with number %s.' % x)
loop.create_task(run_new_vm(manager, loop, n))
def run_tests():
loop = asyncio.get_event_loop()
m = Manager()
loop.run_until_complete(one_service_per_vm(2, m, loop))
loop.close()
我做错了什么?