使用 类 和 Python asyncio 的正确方法
Correct way of using classes with Python asyncio
当使用 Python asyncio 时,处理阻塞 class 的正确方法是什么?
例如:
import asyncio
import time
class getImage(object):
def __init__(self,):
time.sleep(0.5)
self.image_sim = "image"
async def main():
print('starting main loop')
func1 = asyncio.create_task(my_func1())
print("Waiting for func1 to complete")
await func1
print('finished main loop')
async def my_func1():
print("starting func1")
image = getImage()
print(image.image_sim)
print('done func 1')
asyncio.run(main(), debug=True)
这是正确的,还是有更多的'asyncio'方法来做到这一点?
我尝试将 async
添加到 init
函数中,稍后再添加 await
:
class getImage(object):
async def __init__(self,):
time.sleep(0.05)
self.image_sim = "image"
async def main():
print('starting main loop')
func1 = asyncio.create_task(my_func1())
print("Waiting for func1 to complete")
await func1
print('finished main loop')
async def my_func1():
print("starting func1")
image = getImage()
await image
print(image.image_sim)
print('done func 1')
asyncio.run(main(), debug=True)
您可以在 getImage
中使用 async
classmethod
。 classmethod
可以执行设置 image_sim
属性之前所需的 0.5 秒延迟:
import asyncio
class getImage(object):
def __init__(self, img):
self.image_sim = img
@classmethod
async def get_image(cls):
await asyncio.sleep(0.5) #sleep for 0.5 seconds
return cls('image') #return instance of getImage with image_sim set to "image"
async def main():
print('starting main loop')
func1 = asyncio.create_task(my_func1())
print("Waiting for func1 to complete")
await func1
print('finished main loop')
async def my_func1():
print("starting func1")
image = await getImage.get_image() #await the async classmethod of getImage(object)
print(image.image_sim)
print('done func 1')
asyncio.run(main(), debug=True)
当使用 Python asyncio 时,处理阻塞 class 的正确方法是什么?
例如:
import asyncio
import time
class getImage(object):
def __init__(self,):
time.sleep(0.5)
self.image_sim = "image"
async def main():
print('starting main loop')
func1 = asyncio.create_task(my_func1())
print("Waiting for func1 to complete")
await func1
print('finished main loop')
async def my_func1():
print("starting func1")
image = getImage()
print(image.image_sim)
print('done func 1')
asyncio.run(main(), debug=True)
这是正确的,还是有更多的'asyncio'方法来做到这一点?
我尝试将 async
添加到 init
函数中,稍后再添加 await
:
class getImage(object):
async def __init__(self,):
time.sleep(0.05)
self.image_sim = "image"
async def main():
print('starting main loop')
func1 = asyncio.create_task(my_func1())
print("Waiting for func1 to complete")
await func1
print('finished main loop')
async def my_func1():
print("starting func1")
image = getImage()
await image
print(image.image_sim)
print('done func 1')
asyncio.run(main(), debug=True)
您可以在 getImage
中使用 async
classmethod
。 classmethod
可以执行设置 image_sim
属性之前所需的 0.5 秒延迟:
import asyncio
class getImage(object):
def __init__(self, img):
self.image_sim = img
@classmethod
async def get_image(cls):
await asyncio.sleep(0.5) #sleep for 0.5 seconds
return cls('image') #return instance of getImage with image_sim set to "image"
async def main():
print('starting main loop')
func1 = asyncio.create_task(my_func1())
print("Waiting for func1 to complete")
await func1
print('finished main loop')
async def my_func1():
print("starting func1")
image = await getImage.get_image() #await the async classmethod of getImage(object)
print(image.image_sim)
print('done func 1')
asyncio.run(main(), debug=True)