Turn start_rpc_server into a context manager

This commit is contained in:
link2xt
2022-12-01 16:36:36 +00:00
parent 53d6807e8d
commit 85b4746516
3 changed files with 44 additions and 38 deletions

View File

@@ -7,7 +7,7 @@ import deltachat_rpc_client as dc
async def main():
rpc = await dc.start_rpc_server()
async with dc.start_rpc_server() as rpc:
deltachat = dc.Deltachat(rpc)
system_info = await deltachat.get_system_info()
logging.info("Running deltachat core %s", system_info["deltachat_core_version"])

View File

@@ -2,6 +2,7 @@ import asyncio
import json
import logging
import os
from contextlib import asynccontextmanager
import aiohttp
@@ -73,6 +74,7 @@ class Rpc:
return method
@asynccontextmanager
async def start_rpc_server(*args, **kwargs):
proc = await asyncio.create_subprocess_exec(
"deltachat-rpc-server",
@@ -82,7 +84,10 @@ async def start_rpc_server(*args, **kwargs):
**kwargs
)
rpc = Rpc(proc)
return rpc
try:
yield rpc
finally:
proc.terminate()
async def new_online_account():

View File

@@ -10,9 +10,10 @@ from deltachat_rpc_client import Deltachat
@pytest_asyncio.fixture
async def rpc(tmp_path):
return await deltachat_rpc_client.start_rpc_server(
async with deltachat_rpc_client.start_rpc_server(
env={**os.environ, "DC_ACCOUNTS_PATH": str(tmp_path / "accounts")}
)
) as rpc:
yield rpc
@pytest.mark.asyncio