これは、プロジェクトで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
私は当然、これは動作しません確信しています。また、私はinitでrun_until_complete()
の "普通の" asyncio
関数を使ってみましたが、もちろんinitはそれを返すことはありません。
asyncio
このクラスに属する関数はバックグラウンドで定期的に実行できますが、残りのクラス(run()
)は引き続き同期作業を行いますか?