refactor: replace Trio with asyncio/uvloop

This commit is contained in:
Stefan Stammberger
2021-08-30 19:55:02 +02:00
parent fe79709698
commit d9849d43d2
21 changed files with 332 additions and 317 deletions
+15 -14
View File
@@ -1,5 +1,5 @@
import time
import trio
import asyncio
import traceback
from http import HTTPStatus
from typing import List, Callable
@@ -24,21 +24,21 @@ def record_async(func: Callable) -> Callable:
return recorder
def run_deferred_async():
async def run_deferred_async():
for func in deferred_async:
current_app.nursery.start_soon(catch_everything_and_restart, func)
asyncio.create_task(catch_everything_and_restart(func))
async def catch_everything_and_restart(func):
try:
await func()
except trio.Cancelled:
except asyncio.CancelledError:
raise # because we must pass this up
except Exception as exc:
print("caught exception in background task:", exc)
print(traceback.format_exc())
print("will restart the task in 5 seconds.")
await trio.sleep(5)
await asyncio.sleep(5)
await catch_everything_and_restart(func)
@@ -46,10 +46,10 @@ async def send_push_promise(a, b) -> None:
pass
invoice_listeners: List[trio.MemorySendChannel] = []
invoice_listeners: List[asyncio.Queue] = []
def register_invoice_listener(send_chan: trio.MemorySendChannel):
def register_invoice_listener(send_chan: asyncio.Queue):
"""
A method intended for extensions to call when they want to be notified about
new invoice payments incoming.
@@ -64,18 +64,19 @@ async def webhook_handler():
return "", HTTPStatus.NO_CONTENT
internal_invoice_paid, internal_invoice_received = trio.open_memory_channel(0)
internal_invoice_queue = asyncio.Queue(0)
async def internal_invoice_listener():
async for checking_id in internal_invoice_received:
current_app.nursery.start_soon(invoice_callback_dispatcher, checking_id)
while True:
checking_id = await internal_invoice_queue.get()
asyncio.create_task(invoice_callback_dispatcher(checking_id))
async def invoice_listener():
async for checking_id in WALLET.paid_invoices_stream():
print("> got a payment notification", checking_id)
current_app.nursery.start_soon(invoice_callback_dispatcher, checking_id)
asyncio.create_task(invoice_callback_dispatcher(checking_id))
async def check_pending_payments():
@@ -99,7 +100,7 @@ async def check_pending_payments():
# that will be handled by the global invoice listeners, hopefully
incoming = False
await trio.sleep(60 * 30) # every 30 minutes
await asyncio.sleep(60 * 30) # every 30 minutes
async def perform_balance_checks():
@@ -107,7 +108,7 @@ async def perform_balance_checks():
for bc in await get_balance_checks():
redeem_lnurl_withdraw(bc.wallet, bc.url)
await trio.sleep(60 * 60 * 6) # every 6 hours
await asyncio.sleep(60 * 60 * 6) # every 6 hours
async def invoice_callback_dispatcher(checking_id: str):
@@ -115,4 +116,4 @@ async def invoice_callback_dispatcher(checking_id: str):
if payment and payment.is_in:
await payment.set_pending(False)
for send_chan in invoice_listeners:
await send_chan.send(payment)
await send_chan.put(payment)