Collecting Cleanup With AsyncDisposableStack
Priya’s final main() connects a press’s queue only when the first job for that press comes up,
so the runner can’t declare its queues in advance. Python collects them on an AsyncExitStack:
callback(remove_spool, folder) registers the folder’s removal with its argument, and
enter_async_context() connects each queue and adds it. When the async with block ends,
everything is released last first.
JavaScript’s counterpart is the built-in AsyncDisposableStack. The stack is an async
disposable itself, so you declare it with await using in a block of its own, and the summary
prints after the block, as Python’s prints after the async with.
mkdtemp() from
node:fs/promises makes the spool folder, like tempfile.mkdtemp(), but only Python’s puts it in
the temporary folder for you, so the prefix is joined to tmpdir() (from node:os, like
tempfile.gettempdir()) with join() (from node:path, like os.path.join()). removeSpool()
deletes it with rm(folder, { recursive: true }), like shutil.rmtree().
stack.use() is enter_async_context(), except that it doesn’t connect anything: it takes a
queue that’s already open, adds it to the stack and returns it, so it can go straight into the
Map. stack.defer() is callback(), except that it takes one function and calls it with no
arguments. Ported line for line, stack.defer(removeSpool, folder) runs the whole morning,
closes both queues and then stops.