mirror of
https://github.com/chatmail/core.git
synced 2026-05-16 21:36:30 +03:00
allow to use the JSON RPC client from async code
This commit is contained in:
@@ -48,3 +48,7 @@ $ python
|
|||||||
'awesome'
|
'awesome'
|
||||||
>>> rpc.close()
|
>>> rpc.close()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Usage from async code
|
||||||
|
|
||||||
|
See the [echobot_async.py](./examples/echobot_async.py) example.
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
@@ -35,6 +36,10 @@ class Rpc:
|
|||||||
self.writer_thread: Thread
|
self.writer_thread: Thread
|
||||||
self.events_thread: Thread
|
self.events_thread: Thread
|
||||||
|
|
||||||
|
def get_async_rpc(self) -> "AsyncRpc":
|
||||||
|
"""Get asynchronous wrapper to use the RPC methods from async code."""
|
||||||
|
return AsyncRpc(self)
|
||||||
|
|
||||||
def start(self) -> None:
|
def start(self) -> None:
|
||||||
if sys.version_info >= (3, 11):
|
if sys.version_info >= (3, 11):
|
||||||
self.process = subprocess.Popen(
|
self.process = subprocess.Popen(
|
||||||
@@ -84,6 +89,13 @@ class Rpc:
|
|||||||
def __exit__(self, _exc_type, _exc, _tb):
|
def __exit__(self, _exc_type, _exc, _tb):
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
|
async def __aenter__(self):
|
||||||
|
self.__enter__()
|
||||||
|
return AsyncRpc(self)
|
||||||
|
|
||||||
|
async def __aexit__(self, _exc_type, _exc, _tb):
|
||||||
|
self.__exit__(_exc_type, _exc, _tb)
|
||||||
|
|
||||||
def reader_loop(self) -> None:
|
def reader_loop(self) -> None:
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
@@ -165,3 +177,19 @@ class Rpc:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
return method
|
return method
|
||||||
|
|
||||||
|
|
||||||
|
class AsyncRpc:
|
||||||
|
def __init__(self, sync_rpc: Rpc) -> None:
|
||||||
|
self._sync_rpc = sync_rpc
|
||||||
|
|
||||||
|
def __getattr__(self, attr: str) -> Any:
|
||||||
|
sync_method = getattr(self._sync_rpc, attr)
|
||||||
|
if sync_method:
|
||||||
|
|
||||||
|
async def method(*args) -> Any:
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
return await loop.run_in_executor(None, sync_method, *args)
|
||||||
|
|
||||||
|
return method
|
||||||
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user