ui: add special-purpose atexit functionality
In spite of its longstanding use, Python's built-in atexit code is
not suitable for Mercurial's purposes, for several reasons:
* Handlers run after application code has finished.
* Because of this, the code that runs handlers swallows exceptions
(since there's no possible stacktrace to associate errors with).
If we're lucky, we'll get something spat out to stderr (if stderr
still works), which of course isn't any use in a big deployment
where it's important that exceptions get logged and aggregated.
* Mercurial's current atexit handlers make unfortunate assumptions
about process state (specifically stdio) that, coupled with the
above problems, make it impossible to deal with certain categories
of error (try "hg status > /dev/full" on a Linux box).
* In Python 3, the atexit implementation is completely hidden, so
we can't hijack the platform's atexit code to run handlers at a
time of our choosing.
As a result, here's a perfectly cromulent atexit-like implementation
over which we have control. This lets us decide exactly when the
handlers run (after each request has completed), and control what
the process state is when that occurs (and afterwards).
Test UI worker interaction
$ cat > t.py <<EOF
> from __future__ import absolute_import, print_function
> from mercurial import (
> cmdutil,
> ui as uimod,
> worker,
> )
> def runme(ui, args):
> for arg in args:
> ui.status('run\n')
> yield 1, arg
> cmdtable = {}
> command = cmdutil.command(cmdtable)
> @command('test', [], 'hg test [COST]')
> def t(ui, repo, cost=1.0):
> cost = float(cost)
> ui.status('start\n')
> runs = worker.worker(ui, cost, runme, (ui,), range(8))
> for n, i in runs:
> pass
> ui.status('done\n')
> EOF
$ abspath=`pwd`/t.py
$ hg init
Run tests with worker enable by forcing a heigh cost
$ hg --config "extensions.t=$abspath" test 100000.0
start
run
run
run
run
run
run
run
run
done
Run tests without worker by forcing a low cost
$ hg --config "extensions.t=$abspath" test 0.0000001
start
run
run
run
run
run
run
run
run
done