使用Python 3.6 asyncio运行任务asynchrounous

时间:2017-10-25 15:11:36

标签: python python-3.x python-asyncio

我开始研究与AWS Boto通信的新Python 3.6项目。由于启动EC2实例需要一些时间,所以我开始使用from tkinter import * class Application(Frame): def __init__(self, master): super(Application, self).__init__(master) self.grid() self.create_widgets() def create_widgets(self): Label(self, text = "Enter information for services you need on your car" ).grid(row = 0, column = 0, columnspan = 2, sticky = W) #Oil Change Label(self, text = "Oil Change" ).grid(row = 2, column = 0, sticky = W) self.is_itchy = BooleanVar() Checkbutton(self, text = "$26.00", #error here variable = self.oil ).grid(row = 2, column = 1, sticky = W) #Lube Job Label(self, text = "Lube Job" ).grid(row = 3, column = 0, sticky = W) self.is_itchy = BooleanVar() Checkbutton(self, text = "$18.00", variable = self.is_itchy ).grid(row = 3, column = 1, sticky = W) #Radiator Flush Label(self, text = "Radiator Flush" ).grid(row = 4, column = 0, sticky = W) self.is_itchy = BooleanVar() Checkbutton(self, text = "$30.00", variable = self.is_itchy ).grid(row = 4, column = 1, sticky = W) #Transmission Flush Label(self, text = "Oil Change" ).grid(row = 5, column = 0, sticky = W) self.is_itchy = BooleanVar() Checkbutton(self, text = "$80.00", variable = self.is_itchy ).grid(row = 5, column = 1, sticky = W) #Inspection Label(self, text = "Inspection" ).grid(row = 6, column = 0, sticky = W) self.is_itchy = BooleanVar() Checkbutton(self, text = "$15.00", variable = self.is_itchy ).grid(row = 6, column = 1, sticky = W) #Muffler Replacement Label(self, text = "Muffler Replacement" ).grid(row = 7, column = 0, sticky = W) self.is_itchy = BooleanVar() Checkbutton(self, text = "$100.00", variable = self.is_itchy ).grid(row = 7, column = 1, sticky = W) #Tire Rotation Label(self, text = "Tire Rotation" ).grid(row = 8, column = 0, sticky = W) self.is_itchy = BooleanVar() Checkbutton(self, text = "$20.00", variable = self.is_itchy ).grid(row = 8, column = 1, sticky = W) #Buttons Button(self, text = "Click for total price", command = self.tell_story ).grid(row = 9, column = 0, sticky = W) self.story_txt = Text(self, width = 35, height = 5, wrap = WORD) self.story_txt.grid(row = 10, column = 0, columnspan = 3) Button(self, text = "Quit", command = quit ).grid(row = 9, column = 1, sticky = W) def tell_story(self): """ Fill text box with new story based on user input. """ # get values from the GUI if self.oil.get(): print("Goofus") # create the story story = Price # display the story self.story_txt.delete(0.0, END) self.story_txt.insert(0.0, story) root = Tk() root.title("Joe's repair shop") app = Application(root) root.mainloop() 库,但是我遇到了一些麻烦但是理解它。

我想异步启动2个EC2实例。但是,如果我拨打Traceback (most recent call last): File "C:\Users\Kevin Holstein\Desktop\Classes\Python\Labs\Lab 10\Repair shop kholstein.py", line 127, in <module> app = Application(root) File "C:\Users\Kevin Holstein\Desktop\Classes\Python\Labs\Lab 10\Repair shop kholstein.py", line 8, in __init__ self.create_widgets() File "C:\Users\Kevin Holstein\Desktop\Classes\Python\Labs\Lab 10\Repair shop kholstein.py", line 24, in create_widgets variable = self.oil AttributeError: 'Application' object has no attribute 'oil' ,我会收到以下错误:

  

错误:asyncio:任务已被销毁,但它正在等待!

这是我目前的代码:

asyncio

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您正在使用create_task在循环上安排作业,但没有任何东西等待它们完成。 one_service_per_vm将立即返回。

对于asyncio.gather

的许多任务,您可以await
# ...

async def one_service_per_vm(n, manager, loop):
    tasks = [run_new_vm(manager, loop, n) for x in range (0, n)]
    await asyncio.gather(*tasks, loop=loop)


def run_tests():
    loop = asyncio.get_event_loop()
    m = Manager()
    loop.run_until_complete(one_service_per_vm(2, m, loop))
    loop.close()