Skip to content

Commit

Permalink
CABI refactor: improve call_and_handle_blocking interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewagner committed Oct 4, 2024
1 parent 11fe9a6 commit ea387d8
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions design/mvp/canonical-abi/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,22 @@ async def default_on_block(f):
await current_task.acquire()
return v

async def call_and_handle_blocking(callee):
class Blocked: pass

async def call_and_handle_blocking(callee, *args) -> Blocked|any:
blocked = asyncio.Future()
async def on_block(f):
if not blocked.done():
blocked.set_result(True)
blocked.set_result(Blocked())
else:
current_task.release()
v = await f
await current_task.acquire()
return v
async def do_call():
await callee(on_block)
ret = await callee(*args, on_block)
if not blocked.done():
blocked.set_result(False)
blocked.set_result(ret)
else:
current_task.release()
asyncio.create_task(do_call())
Expand Down Expand Up @@ -1454,13 +1456,14 @@ async def canon_lower(opts, ft, callee, task, flat_args):
async def do_call(on_block):
await callee(task, subtask.on_start, subtask.on_return, on_block)
[] = subtask.finish()
if await call_and_handle_blocking(do_call):
subtask.notify_supertask = True
task.need_to_drop += 1
i = task.inst.async_subtasks.add(subtask)
flat_results = [pack_async_result(i, subtask.state)]
else:
flat_results = [0]
match await call_and_handle_blocking(do_call):
case Blocked():
subtask.notify_supertask = True
task.need_to_drop += 1
i = task.inst.async_subtasks.add(subtask)
flat_results = [pack_async_result(i, subtask.state)]
case _:
flat_results = [0]
return flat_results

def pack_async_result(i, state):
Expand Down

0 comments on commit ea387d8

Please sign in to comment.