commando: actually do RPC dispatch async.

We use multiprocessing for this, because the pyln-client RPC
command api doesn't support async.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2022-03-17 14:02:18 +10:30
parent efaf634f1b
commit 5b2dd9a3f6

View File

@@ -26,6 +26,7 @@ import random
import secrets
import string
import runes # type: ignore
import multiprocessing
from typing import Dict, Tuple, Optional
plugin = Plugin()
@@ -132,6 +133,15 @@ def do_cacherune(plugin, peer_id, runestr):
return {'result': {'rune': runestr}}
def command_run(plugin, peer_id, idnum, method, params):
"""Function to run a command and write the result"""
try:
res = {'result': plugin.rpc.call(method, params)}
except RpcError as e:
res = {'error': e.error}
send_result(plugin, peer_id, idnum, res)
def try_command(plugin, peer_id, idnum, method, params, runestr):
"""Run an arbitrary command and message back the result"""
# You can always set your rune, even if *that rune* wouldn't
@@ -153,10 +163,11 @@ def try_command(plugin, peer_id, idnum, method, params, runestr):
else:
res = {'error': 'FIXME: Refusing to call inside ourselves'}
else:
try:
res = {'result': plugin.rpc.call(method, params)}
except RpcError as e:
res = {'error': e.error}
# The subprocess does send_result itself: pyln-client doesn't
# support async RPC yet.
multiprocessing.Process(target=command_run,
args=(plugin, peer_id, idnum, method, params)).start()
return
send_result(plugin, peer_id, idnum, res)