on_next
次のように、Pythonrx
サブスクリプション内で非同期関数を呼び出す必要があります。
from rx.subject import Subject
import asyncio
async def asyncPrint(value: str):
print(f'async print: {value}')
async def main():
s = Subject()
s.subscribe(
on_error=lambda e: print(e),
on_next=lambda value: asyncPrint(value)
)
s.on_next('Im from the subject')
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())
しかし、非同期エラーが発生します:
$ python test.py
rx\core\observer\autodetachobserver.py:26:
RuntimeWarning: coroutine 'asyncPrint' was never awaited
self._on_next(value)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
asyncio.get_event_loop().run_until_complete(...)
私はすでにメインループを実行していて、新しいループを開始したくないし、ネストされたループも使用したくないので、本当に使いたくありません。
それについて検索したところ、lambda
関数を async にすることはできないことがわかりました。ライブラリon_next
を使用してラムダ関数を使用せずに値を取得する方法が本当にわからないため、ここで問題があると思います。rx
async-rxライブラリについても検索しましたが、これは実行できる唯一の異なることのように見えますが、await subscribe(...)
それは私が望むものではありません。のようなものが欲しいsubscribe(on_next=await...)
。
それは可能ですか?私のバックグラウンドから、内部で関数javascript
を開始するのは簡単なので、私にとって可能なタスクのように見えます。誰かがそれに対する解決策を見つけてくれることを願っています。async
subscribe
どうもありがとう!