이것은 프로젝트에서 asyncio
을 사용하기위한 첫 번째 시도입니다. 내 클래스를 초기화하고 실행하고, 여러 함수가 백그라운드에서 주기적으로 실행되도록하고 싶습니다. 클래스의 init가 이러한 백그라운드 작업을 시작한 후에 돌아와서 동시에 동기 작업을 계속할 수 있기를 바랍니다.asyncio가있는 백그라운드에서 스레드 클래스의 함수 실행
내가 무엇을 가지고 :
class MyClass(threading.Thread):
def __init__(self, param):
self.stoprequest = threading.Event()
threading.Thread.__init__(self)
self.param = param
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
asyncio.ensure_future(self.periodic(), loop=self.loop)
print("Initialized")
async def periodic(self):
while True:
print("I'm here")
await asyncio.sleep(1)
def run(self):
# continue to do synchronous things
나는 당연히 확신이 작동하지 않습니다. 나는 또한 "normal"asyncio
함수를 사용하여 run_until_complete()
을 init에서 시도했지만, 물론 init은 결코 반환하지 않습니다.
나머지 클래스 (run()
)가 동기 작업을 계속하는 동안이 클래스에 속하는 asyncio
함수를 백그라운드에서 주기적으로 실행할 수 있습니까?