RFC: draft idea for making for loops automatically close iterators
Hi all, I've been poking at an idea for changing how 'for' loops work to hopefully make them work better for pypy and async/await code. I haven't taken it to python-ideas yet -- this is its first public outing, actually -- but since it directly addresses pypy GC issues I thought I'd send around a draft to see what you think. (E.g., would this be something that makes your life easier?) Thanks, -n ---- Abstract ======== We propose to extend the iterator protocol with a new ``__(a)iterclose__`` slot, which is called automatically on exit from ``(async) for`` loops, regardless of how they exit. This allows for convenient, deterministic cleanup of resources held by iterators without reliance on the garbage collector. This is especially important (and urgent) for asynchronous generators. Note ==== In practical terms, the proposal here is divided into two separate parts: the handling of async iterators, which should ideally be implemented ASAP, and the handling of regular iterators, which is a larger but more relaxed project that won't even start until 3.7. But since the changes are closely related, and we probably don't want to end up with async iterators and regular iterators diverging in the long run, it seems useful to look at them together. Background and motivation ========================= Python iterables often hold resources which require cleanup. For example: ``file`` objects need to be closed; the `WSGI spec <https://www.python.org/dev/peps/pep-0333/>`_ adds a ``close`` method on top of the regular iterator protocol and demands that consumers call it at the appropriate time (though forgetting to do so is a `frequent source of bugs <http://blog.dscpl.com.au/2012/10/obligations-for-calling-close-on.html>`_); and PEP 342 (based on PEP 325) extended generator objects to add a ``close`` method to allow generators to clean up after themselves. Generally, objects that need to clean up after themselves define a ``__del__`` method to ensure that this cleanup will happen eventually, when the object is garbage collected. However, relying on the garbage collector for cleanup like this causes serious problems in at least two cases: - In Python implementations that do not use reference counting (e.g. PyPy, Jython), calls to ``__del__`` may be arbitrarily delayed, yet many situations depend on *prompt* cleanup of resources. Delayed cleanup produces problems like crashes due to file descriptor exhaustion, or WSGI timing middleware that collects bogus times. - Async generators (PEP 525) can only perform cleanup under the supervision of the appropriate coroutine runner. ``__del__`` doesn't have access to the coroutine runner; indeed, the coroutine runner might be garbage collected before the generator object. So relying on the garbage collector is effectively impossible without some kind of language extension. (PEP 525 does provide such an extension, but it has serious flaws; see the "rejected alternatives" section below.) The usual recommendation, therefore, is to avoid the garbage collector by using ``with`` blocks. For example, this code opens a file but relies on the garbage collector to close it:: def read_newline_separated_json(path): for line in open(path): yield json.loads(line) for document in read_newline_separated_json(path): ... and recent versions of CPython will point this out by issuing a ``ResourceWarning``, nudging us to fix it by adding a ``with`` block:: def read_newline_separated_json(path): with open(path) as file_handle: # <-- new for line in file_handle: yield json.loads(line) for document in read_newline_separated_json(path): # <-- outer for loop ... But there's a subtlety here, caused by the interaction of ``with`` blocks and generators. ``with`` blocks are Python's main tool for ensuring cleanup, and they're a powerful one, because they pin the lifetime of a resource to the lifetime of a stack frame. But in the case of generators, we need a ``with`` block to ensure that the stack frame is cleaned up! In this case, adding the ``with`` block *is* enough to shut up the ``ResourceWarning``, but this is misleading -- the file object cleanup here is still dependent on the garbage collector. The ``with`` block will only be unwound when the ``read_newline_separated_json`` generator is closed. If the outer ``for`` loop runs to completion then the cleanup will happen immediately; but if this loop is terminated early by a ``break`` or an exception, then the ``with`` block won't fire until the generator object is garbage collected. The correct solution requires that all *users* of this API wrap every ``for`` loop in its own ``with`` block:: with closing(read_newline_separated_json(path)) as genobj: for document in genobj: ... This gets even worse if we consider the idiom of decomposing a complex pipeline into multiple nested generators:: def read_users(path): with closing(read_newline_separated_json(path)) as gen: for document in gen: yield User.from_json(document) def users_in_group(path, group): with closing(read_users(path)) as gen: for user in gen: if user.group == group: yield user In general if you have N nested generators then you need N+1 ``with`` blocks to clean up 1 file. And good defensive programming would suggest that any time we use a generator, we should assume the possibility that there could be at least one ``with`` block somewhere in its (potentially transitive) call stack, either now or in the future, and thus always wrap it in a ``with``. But in practice, basically nobody does this, because it's awful and programmers would rather write buggy code than tiresome repetitive code. Is this worth fixing? Previously I would have argued yes, but that it was a low priority -- until the advent of async generators, which makes this problem much more urgent. Async generators cannot do cleanup *at all* without some mechanism for deterministic cleanup that people will actually use, and async generators are particularly likely to hold resources like file descriptors. (After all, if they weren't doing I/O, they'd be generators, not async generators.) And if we don't get it right now when async generators are first rolling out, then it'll be much harder to fix later. The proposal itself is simple in concept: add a ``__(a)iterclose__`` method to the iterator protocol, and have (async) ``for`` loops call it when the loop is exited. Effectively, we're taking the current cumbersome idiom (``with`` block + ``for`` loop) and merging them together into a fancier ``for``. Rejected alternatives ===================== PEP 525 asyncgen hooks ---------------------- PEP 525 proposes a `set of global hooks managed by new ``sys.{get/set}_asyncgen_hooks()`` functions <https://www.python.org/dev/peps/pep-0525/#finalization>`_, which event loops are intended to register to take control of async generator finalization. The stated goal is that "the end user does not need to care about the finalization problem, and everything just works". Unfortunately, though, the approach has a number of downsides: - It adds substantial complexity: we have new global interpreter state, and new public API in asyncio (``loop.shutdown_asyncgens()``) that users have to remember to call at the appropriate time. - The ``firstiter`` hook has to be able to uniquely identify what coroutine runner is being used at any given moment, and the ``finalizer`` hook has to be able to take a generator object and figure out which coroutine runner was supervising it initially. These requirements introduce surprisingly complicated couplings and potential constraints on future designs. For example, one might plausibly want to start several OS threads, and run a separate asyncio event loop in each -- ``asyncio.BaseEventLoopPolicy`` takes some trouble to support exactly this use case. But once there are multiple event loops running simultaneously, the hooks have the problem of somehow matching up each generator to its corresponding event loop. For ``firstiter`` this isn't so bad -- we can assume that the thread where ``firstiter`` is called is matches the thread whose event loop we want -- but ``finalizer`` is trickier, since the generator might be collected in a different thread than it started in. The code currently in the asyncio master branch doesn't consider this situation at all. If you try, what will happen is that whichever event loop starts up last will run the finalizers for all threads, which will probably blow up spectacularly. The current implementation is also broken if the following sequence of events occurs: 1. start a loop 2. firstiter(agen) invoked 3. stop the loop, but forget to call ``loop.shutdown_asyncgens()``. (NB: No existing asyncio programs call ``loop.shutdown_asyncgens()``, and it's never called automatically.) 4. create a new loop 5. finalizer(agen) invoked -- now the new loop will happily attempt to execute agen.aclose() These issues with the current implementation are fixable (XX FIXME file bugs), but they give a sense of how tricky this API is. It gets worse: suppose I want to run an asyncio event loop in one thread and a twisted reactor loop in another (e.g., to take advantage of twisted functionality that hasn't yet been ported to run on top of asyncio, with communication between the threads using ``call_soon_threadsafe`` / ``callFromThread``). Now the two event loops have to fight over the hooks. Curio currently doesn't even have the concept of a global event loop. A more obscure case arises with libraries like `async_generator <https://pypi.python.org/pypi/async_generator>`_, which runs code under a "proxy" coroutine runner that handles some yields itself while forwarding others on to the real event loop. Here it is the *inner* coroutine runner that should be used for calling ``aclose``, not the outer one, but there is no way for the hooks to know this. Though obviously this isn't a problem for async_generator itself since it's obsoleted by PEP 525, and it's not clear whether this technique has other use cases. But on the other hand, maybe we should try to keep our options open; we have so little experience with async/await that it's hard to say what clever tricks will turn out to be important. Basically the point is, these hooks have extremely delicate semantics and it's not at all clear that we know how to deal with all the situations they cause. - The new semantics aren't part of the abstract async iterator protocol, but are instead tied `specifically to the async generator concrete type <XX find and link Yury's email saying this>`_. If you have an async iterator implemented using a class, like:: class MyAIterator: def __anext__(): ... then you can't refactor this into an async generator without changing the semantics, and vice-versa. This seems very unpythonic. (It also leaves open the question of what exactly class-based async iterators are supposed to do, given that since they face exactly the same cleanup problems as async generators.) And then assuming we manage to avoid the problems above, the best-case payoff is that we get GC semantics for async generators. So after all that it's still effectively a CPython-only feature (!!), and even there it has poor ergonomics, e.g., if ``aclose`` raises an error then it will get lost. In practice, code that wants to be portable across Python implementations or handle exceptions reliably will still have to write things like:: with aclosing(get_newline_separated_json(url)) as agen: async for document in agen: ... just like it would if the asyncgen hooks didn't exist. By comparison, the present proposal is straightforward and understandable, requires no global state or global coordination between coroutine runners, works equally well for generators and other iterators, works on PyPy, gives properly propagating exceptions by default, etc. Always inject resources, and do all cleanup at the top level ------------------------------------------------------------ It was suggested on python-dev (XX find link) that a pattern to avoid these problems is to always pass resources in from above, e.g. ``read_newline_separated_json`` should take a file object rather than a path, with cleanup handled at the top level:: def read_newline_separated_json(file_handle): for line in file_handle: yield json.loads(line) def read_users(file_handle): for document in read_newline_separated_json(file_handle): yield User.from_json(document) with open(path) as file_handle: for user in read_users(file_handle): ... This works well in simple cases; here it lets us avoid the "N+1 problem". But unfortunately, it breaks down quickly when things get more complex. Consider if instead of reading from a file, our generator was processing the body returned by an HTTP GET request -- while handling redirects and authentication via OAUTH. Then we'd really want the sockets to be managed down inside our HTTP client library, not at the top level. Plus there are other cases where ``finally`` blocks embedded inside generators are important in their own right: db transaction management, emitting logging information during cleanup (one of the major motivating use cases for WSGI ``close``), and so forth. Specification: final state ========================== This section describes where we want to eventually end up, though there are some backwards compatibility issues that mean we can't jump directly here. A later section describes the transition plan. Guiding principles ------------------ Generally, ``__(a)iterclose__`` implementations should: - be idempotent, - perform any cleanup that is appropriate on the assumption that the iterator will not be used again after ``__(a)iterclose__`` is called. In particular, once ``__(a)iterclose__`` has been called then calling ``__(a)next__`` produces undefined behavior. And generally, any code which starts iterating through an iterable with the intention of exhausting it, should arrange to make sure that ``__(a)iterclose__`` is eventually called, whether or not the iterator is actually exhausted. Changes to iteration -------------------- The core proposal is the change in behavior of ``for`` loops. Given this Python code:: for VAR in ITERABLE: LOOP-BODY else: ELSE-BODY we desugar to the equivalent of:: _iter = iter(ITERABLE) _iterclose = getattr(type(_iter), "__iterclose__", lambda: None) try: traditional-for VAR in _iter: LOOP-BODY else: ELSE-BODY finally: _iterclose(_iter) where the "traditional-for statement" here is meant as a shorthand for the classic 3.5-and-earlier ``for`` loop semantics. Besides the top-level ``for`` statement, Python also contains several other places where iterators are consumed. For consistency, these should call ``__iterclose__`` as well using semantics equivalent to the above. This includes: - ``for`` loops inside comprehensions - ``*`` unpacking - functions which accept and fully consume iterables, like ``list(it)``, ``tuple(it)``, ``itertools.product(it1, it2, ...)``, and others. Changes to async iteration -------------------------- We also make the analogous changes to async iteration constructs, except that the new slot is called ``__aiterclose__``, and it's an async method that gets ``await``\ed. Modifications to basic iterator types ------------------------------------- Generator objects (including those created by generator comprehensions): - ``__iterclose__`` calls ``self.close()`` - ``__del__`` calls ``self.close()`` (same as now), and additionally issues a ``ResourceWarning`` if ``aclose`` has not been called. This warning is hidden by default, but can be enabled for those who want to make sure they aren't inadverdantly relying on CPython-specific GC semantics. Async generator objects (including those created by async generator comprehensions): - ``__aiterclose__`` calls ``self.aclose()`` - ``__del__`` issues a ``RuntimeWarning`` if ``aclose`` has not been called, since this probably indicates a latent bug, similar to the "coroutine never awaited" warning. OPEN QUESTION: should file objects implement ``__iterclose__`` to close the file? On the one hand this would make this change more disruptive; on the other hand people really like writing ``for line in open(...): ...``, and if we get used to iterators taking care of their own cleanup then it might become very weird if files don't. New convenience functions ------------------------- The ``itertools`` module gains a new iterator wrapper that can be used to selectively disable the new ``__iterclose__`` behavior:: # XX FIXME: I feel like there might be a better name for this one? class protect(iterable): def __init__(self, iterable): self._it = iter(iterable) def __iter__(self): return self def __next__(self): return next(self._it) def __iterclose__(self): # Swallow __iterclose__ without passing it on pass Example usage (assuming that file objects implements ``__iterclose__``):: with open(...) as handle: # Iterate through the same file twice: for line in itertools.protect(handle): ... handle.seek(0) for line in itertools.protect(handle): ... The ``operator`` module gains two new functions, with semantics equivalent to the following:: def iterclose(it): if hasattr(type(it), "__iterclose__"): type(it).__iterclose__(it) async def aiterclose(ait): if hasattr(type(ait), "__aiterclose__"): await type(ait).__aiterclose__(ait) These are particularly useful when implementing the changes in the next section: __iterclose__ implementations for iterator wrappers --------------------------------------------------- Python ships a number of iterator types that act as wrappers around other iterators: ``map``, ``zip``, ``itertools.accumulate``, ``csv.reader``, and others. These iterators should define a ``__iterclose__`` method which calls ``__iterclose__`` in turn on their underlying iterators. For example, ``map`` could be implemented as:: class map: def __init__(self, fn, *iterables): self._fn = fn self._iters = [iter(iterable) for iterable in iterables] def __iter__(self): return self def __next__(self): return self._fn(*[next(it) for it in self._iters]) def __iterclose__(self): for it in self._iters: operator.iterclose(it) In some cases this requires some subtlety; for example, ```itertools.tee`` <https://docs.python.org/3/library/itertools.html#itertools.tee>`_ should not call ``__iterclose__`` on the underlying iterator until it has been called on *all* of the clone iterators. Example / Rationale ------------------- The payoff for all this is that we can now write straightforward code like:: def read_newline_separated_json(path): for line in open(path): yield json.loads(line) and be confident that the file will receive deterministic cleanup *without the end-user having to take any special effort*, even in complex cases. For example, consider this silly pipeline:: list(map(lambda key: key.upper(), doc["key"] for doc in read_newline_separated_json(path))) If our file contains a document where ``doc["key"]`` turns out to be an integer, then the following sequence of events will happen: 1. ``key.upper()`` raises an ``AttributeError``, which propagates out of the ``map`` and triggers the implicit ``finally`` block inside ``list``. 2. The ``finally`` block in ``list`` calls ``__iterclose__()`` on the map object. 3. ``map.__iterclose__()`` calls ``__iterclose__()`` on the generator comprehension object. 4. This injects a ``GeneratorExit`` exception into the generator comprehension body, which is currently suspended inside the comprehension's ``for`` loop body. 5. The exception propagates out of the ``for`` loop, triggering the ``for`` loop's implicit ``finally`` block, which calls ``__iterclose__`` on the generator object representing the call to ``read_newline_separated_json``. 6. This injects an inner ``GeneratorExit`` exception into the body of ``read_newline_separated_json``, currently suspended at the ``yield``. 7. The inner ``GeneratorExit`` propagates out of the ``for`` loop, triggering the ``for`` loop's implicit ``finally`` block, which calls ``__iterclose__()`` on the file object. 8. The file object is closed. 9. The inner ``GeneratorExit`` resumes propagating, hits the boundary of the generator function, and causes ``read_newline_separated_json``'s ``__iterclose__()`` method to return successfully. 10. Control returns to the generator comprehension body, and the outer ``GeneratorExit`` continues propagating, allowing the comprehension's ``__iterclose__()`` to return successfully. 11. The rest of the ``__iterclose__()`` calls unwind without incident, back into the body of ``list``. 12. The original ``AttributeError`` resumes propagating. (The details above assume that we implement ``file.__iterclose__``; if not then add a ``with`` block to ``read_newline_separated_json`` and essentially the same logic goes through.) Of course, from the user's point of view, this can be simplified down to just: 1. ``int.upper()`` raises an ``AttributeError`` 1. The file object is closed. 2. The ``AttributeError`` propagates out of ``list`` So we've accomplished our goal of making this "just work" without the user having to think about it. Specification: how to get there from here ========================================= While the majority of existing ``for`` loops will continue to produce identical results, the proposed changes will produce backwards-incompatible behavior in some cases. Example:: def read_csv_with_header(lines_iterable): lines_iterator = iter(lines_iterable) # Used to be correct; now needs an itertools.protect() here: for line in lines_iterator: column_names = line.strip().split("\t") break for line in lines_iterator: values = line.strip().split("\t") record = dict(zip(column_names, values)) yield record Specifically, the incompatibility happens when all of these factors come together: - The automatic calling of ``__(a)iterclose__`` is enabled - The iterable did not previously define ``__(a)iterclose__`` - The iterable does now define ``__(a)iterclose__`` - The iterable is re-used after the ``for`` loop exits So the problem is how to manage this transition, and those are the levers we have to work with. First, observe that the only async iterables where we propose to add ``__aiterclose__`` are async generators, and there is currently no existing code using async generators (though this will start changing very soon), so the async changes do not produce any backwards incompatibilities. (There is existing code using async iterators, but using the new async for loop on an old async iterator is harmless, because old async iterators don't have ``__aiterclose__``.) In addition, PEP 525 was accepted on a provisional basis, and async generators are by far the biggest beneficiary of this PEP's proposed changes. Therefore, I think we should strongly consider enabling ``__aiterclose__`` for ``async for`` loops and async generators ASAP, ideally for 3.6.0 or 3.6.1. For the non-async world, things are harder, but here's a potential transition path: In 3.7: Our goal is that existing unsafe code will start emitting warnings, while those who want to opt-in to the future can do that immediately: - We immediately add all the ``__iterclose__`` methods described above. - If ``from __future__ import iterclose`` is in effect, then ``for`` loops and ``*`` unpacking call ``__iterclose__`` as specified above. - If the future is *not* enabled, then ``for`` loops and ``*`` unpacking do *not* call ``__iterclose__``. But they do call some other method instead, e.g. ``__iterclose_warning__``. - Similarly, functions like ``list`` use stack introspection (!!) to check whether their direct caller has ``__future__.iterclose`` enabled, and use this to decide whether to call ``__iterclose__`` or ``__iterclose_warning__``. - For all the wrapper iterators, we also add ``__iterclose_warning__`` methods that forward to the ``__iterclose_warning__`` method of the underlying iterator or iterators. - For generators (and files, if we decide to do that), ``__iterclose_warning__`` is defined to set an internal flag, and other methods on the object are modified to check for this flag. If they find the flag set, they issue a ``PendingDeprecationWarning`` to inform the user that in the future this sequence would have led to a use-after-close situation and the user should use ``protect()``. In 3.8: - Switch from ``PendingDeprecationWarning`` to ``DeprecationWarning`` In 3.9: - Enable the future unconditionally and remove all the ``__iterclose_warning__`` stuff. I believe that this satisfies the normal requirements for this kind of transition -- opt-in initially, with warnings targeted precisely to the cases that will be effected, and a long deprecation cycle. Probably the most controversial / risky part of this is the use of stack introspection to make the iterable-consuming functions sensitive to a ``__future__`` setting, though I haven't thought of any situation where it would actually go wrong yet... -- Nathaniel J. Smith -- https://vorpus.org
On 17 October 2016 at 09:08, Nathaniel Smith <njs@pobox.com> wrote:
Hi all,
I've been poking at an idea for changing how 'for' loops work to hopefully make them work better for pypy and async/await code. I haven't taken it to python-ideas yet -- this is its first public outing, actually -- but since it directly addresses pypy GC issues I thought I'd send around a draft to see what you think. (E.g., would this be something that makes your life easier?)
To be clear, I'm not a PyPy dev so I'm just answering from a general Python perspective here.
Always inject resources, and do all cleanup at the top level ------------------------------------------------------------
It was suggested on python-dev (XX find link) that a pattern to avoid these problems is to always pass resources in from above, e.g. ``read_newline_separated_json`` should take a file object rather than a path, with cleanup handled at the top level::
I suggested this and I still think that it is the best idea.
def read_newline_separated_json(file_handle): for line in file_handle: yield json.loads(line)
def read_users(file_handle): for document in read_newline_separated_json(file_handle): yield User.from_json(document)
with open(path) as file_handle: for user in read_users(file_handle): ...
This works well in simple cases; here it lets us avoid the "N+1 problem". But unfortunately, it breaks down quickly when things get more complex. Consider if instead of reading from a file, our generator was processing the body returned by an HTTP GET request -- while handling redirects and authentication via OAUTH. Then we'd really want the sockets to be managed down inside our HTTP client library, not at the top level. Plus there are other cases where ``finally`` blocks embedded inside generators are important in their own right: db transaction management, emitting logging information during cleanup (one of the major motivating use cases for WSGI ``close``), and so forth.
I haven't written the kind of code that you're describing so I can't say exactly how I would do it. I imagine though that helpers could be used to solve some of the problems that you're referring to though. Here's a case I do know where the above suggestion is awkward: def concat(filenames): for filename in filenames: with open(filename) as inputfile: yield from inputfile for line in concat(filenames): ... It's still possible to safely handle this use case by creating a helper though. fileinput.input almost does what you want: with fileinput.input(filenames) as lines: for line in lines: ... Unfortunately if filenames is empty this will default to sys.stdin so it's not perfect but really I think introducing useful helpers for common cases (rather than core language changes) should be considered as the obvious solution here. Generally it would have been better if the discussion for PEP 525 has focussed more on helping people to debug/fix dependence on __del__ rather than trying to magically fix broken code.
New convenience functions -------------------------
The ``itertools`` module gains a new iterator wrapper that can be used to selectively disable the new ``__iterclose__`` behavior::
# XX FIXME: I feel like there might be a better name for this one? class protect(iterable): def __init__(self, iterable): self._it = iter(iterable)
def __iter__(self): return self
def __next__(self): return next(self._it)
def __iterclose__(self): # Swallow __iterclose__ without passing it on pass
Example usage (assuming that file objects implements ``__iterclose__``)::
with open(...) as handle: # Iterate through the same file twice: for line in itertools.protect(handle): ... handle.seek(0) for line in itertools.protect(handle): ...
It would be much simpler to reverse this suggestion and say let's introduce a helper that selectively *enables* the new behaviour you're proposing i.e.: for line in itertools.closeafter(open(...)): ... if not line.startswith('#'): break # <--------------- file gets closed here Then we can leave (async) for loops as they are and there are no backward compatbility problems etc. -- Oscar
Hi Oscar, Thanks for the comments! Can I ask that you hold onto them until I post to python-ideas, though? (Should be later today.) It's a discussion worth having, but if we have it here then we'll just end up having to repeat it there anyway :-). -n On Mon, Oct 17, 2016 at 5:04 AM, Oscar Benjamin <oscar.j.benjamin@gmail.com> wrote:
On 17 October 2016 at 09:08, Nathaniel Smith <njs@pobox.com> wrote:
Hi all,
I've been poking at an idea for changing how 'for' loops work to hopefully make them work better for pypy and async/await code. I haven't taken it to python-ideas yet -- this is its first public outing, actually -- but since it directly addresses pypy GC issues I thought I'd send around a draft to see what you think. (E.g., would this be something that makes your life easier?)
To be clear, I'm not a PyPy dev so I'm just answering from a general Python perspective here.
Always inject resources, and do all cleanup at the top level ------------------------------------------------------------
It was suggested on python-dev (XX find link) that a pattern to avoid these problems is to always pass resources in from above, e.g. ``read_newline_separated_json`` should take a file object rather than a path, with cleanup handled at the top level::
I suggested this and I still think that it is the best idea.
def read_newline_separated_json(file_handle): for line in file_handle: yield json.loads(line)
def read_users(file_handle): for document in read_newline_separated_json(file_handle): yield User.from_json(document)
with open(path) as file_handle: for user in read_users(file_handle): ...
This works well in simple cases; here it lets us avoid the "N+1 problem". But unfortunately, it breaks down quickly when things get more complex. Consider if instead of reading from a file, our generator was processing the body returned by an HTTP GET request -- while handling redirects and authentication via OAUTH. Then we'd really want the sockets to be managed down inside our HTTP client library, not at the top level. Plus there are other cases where ``finally`` blocks embedded inside generators are important in their own right: db transaction management, emitting logging information during cleanup (one of the major motivating use cases for WSGI ``close``), and so forth.
I haven't written the kind of code that you're describing so I can't say exactly how I would do it. I imagine though that helpers could be used to solve some of the problems that you're referring to though. Here's a case I do know where the above suggestion is awkward:
def concat(filenames): for filename in filenames: with open(filename) as inputfile: yield from inputfile
for line in concat(filenames): ...
It's still possible to safely handle this use case by creating a helper though. fileinput.input almost does what you want:
with fileinput.input(filenames) as lines: for line in lines: ...
Unfortunately if filenames is empty this will default to sys.stdin so it's not perfect but really I think introducing useful helpers for common cases (rather than core language changes) should be considered as the obvious solution here. Generally it would have been better if the discussion for PEP 525 has focussed more on helping people to debug/fix dependence on __del__ rather than trying to magically fix broken code.
New convenience functions -------------------------
The ``itertools`` module gains a new iterator wrapper that can be used to selectively disable the new ``__iterclose__`` behavior::
# XX FIXME: I feel like there might be a better name for this one? class protect(iterable): def __init__(self, iterable): self._it = iter(iterable)
def __iter__(self): return self
def __next__(self): return next(self._it)
def __iterclose__(self): # Swallow __iterclose__ without passing it on pass
Example usage (assuming that file objects implements ``__iterclose__``)::
with open(...) as handle: # Iterate through the same file twice: for line in itertools.protect(handle): ... handle.seek(0) for line in itertools.protect(handle): ...
It would be much simpler to reverse this suggestion and say let's introduce a helper that selectively *enables* the new behaviour you're proposing i.e.:
for line in itertools.closeafter(open(...)): ... if not line.startswith('#'): break # <--------------- file gets closed here
Then we can leave (async) for loops as they are and there are no backward compatbility problems etc.
-- Oscar _______________________________________________ pypy-dev mailing list pypy-dev@python.org https://mail.python.org/mailman/listinfo/pypy-dev
-- Nathaniel J. Smith -- https://vorpus.org
Have you considered Custodians, a-la racket? I suspect that adding resources to and finalising custodians requires less defensiveness than marking all iterables as resources, but I've yet to see someone implement them in python. https://docs.racket-lang.org/reference/custodians.html -- William Leslie Notice: Likely much of this email is, by the nature of copyright, covered under copyright law. You absolutely MAY reproduce any part of it in accordance with the copyright law of the nation you are reading this in. Any attempt to DENY YOU THOSE RIGHTS would be illegal without prior contractual agreement.
Hi, On 17 October 2016 at 10:08, Nathaniel Smith <njs@pobox.com> wrote:
thought I'd send around a draft to see what you think. (E.g., would this be something that makes your life easier?)
As a general rule, PyPy's GC behavior is similar to CPython's if we tweak the program to start a chain of references at a self-referential object. So for example, consider that the outermost loop of a program takes the objects like the async generators, and stores them inside such an object: class A: def __init__(self, ref): self.ref = ref self.myself = self and then immediately forget that A instance. Then both this A instance and everything it refers to is kept alive until the next cyclic GC occurs. PyPy just always exhibits that behavior instead of only when you start with reference cycles. So the real issue should not be "how to so something that will make PyPy happy", or not only---it should be "how to do something that will make CPython happy even in case of reference cycles". If you don't, then arguably CPython is slightly broken. Yes, anything that can reduce file descriptor leaks in Python sounds good to me. A bientôt, Armin.
Well I'm really shocked to find out what I thought was a "automatic close" is really the ref-couting GC of CPython, means that a lot of my code breaks in PyPy... It really becomes a big problem after iterators heavily used in Python nowadays. Some builtin functions like zip, map, filter return iterators in Python 3 instead of lists in Python 2, means invisible bugs for code ported from Python 2, like zip(my_generator(), my_other_generator()) may leave the iterators open if exited from a for loop. Even in Python 2, functions in itertools may create these bugs. In CPython, this kind of code will work because of the ref-counting GC, so it is not obvious in CPython, but they break in PyPy. I'm wondering since a ref-counting GC implemention is not possible for PyPy, is it possible to hack on the for loop to make it "try to" collect the generator? That may really save a lot of lives. If the generator is still referenced after the for loop, it may be the programmer's fault for not calling close(), but loop through a returned value is something different - sometimes you even do not know if it is a generator. 2016-10-21 hubo 发件人:Armin Rigo <armin.rigo@gmail.com> 发送时间:2016-10-18 16:01 主题:Re: [pypy-dev] RFC: draft idea for making for loops automatically close iterators 收件人:"Nathaniel Smith"<njs@pobox.com> 抄送:"PyPy Developer Mailing List"<pypy-dev@python.org> Hi, On 17 October 2016 at 10:08, Nathaniel Smith <njs@pobox.com> wrote:
thought I'd send around a draft to see what you think. (E.g., would this be something that makes your life easier?)
As a general rule, PyPy's GC behavior is similar to CPython's if we tweak the program to start a chain of references at a self-referential object. So for example, consider that the outermost loop of a program takes the objects like the async generators, and stores them inside such an object: class A: def __init__(self, ref): self.ref = ref self.myself = self and then immediately forget that A instance. Then both this A instance and everything it refers to is kept alive until the next cyclic GC occurs. PyPy just always exhibits that behavior instead of only when you start with reference cycles. So the real issue should not be "how to so something that will make PyPy happy", or not only---it should be "how to do something that will make CPython happy even in case of reference cycles". If you don't, then arguably CPython is slightly broken. Yes, anything that can reduce file descriptor leaks in Python sounds good to me. A bientôt, Armin. _______________________________________________ pypy-dev mailing list pypy-dev@python.org https://mail.python.org/mailman/listinfo/pypy-dev
That’s a good point, as it means there’s probably no safe & portable way to ensure that kind of stuff. «Trying to collect» something doesn’t really fall short of an actual collection, I believe (finding referers is hard). But I believe iterclose() defined appropriately on derived iterators would solve that?..
21 окт. 2016 г., в 17:13, hubo <hubo@jiedaibao.com> написал(а):
Well I'm really shocked to find out what I thought was a "automatic close" is really the ref-couting GC of CPython, means that a lot of my code breaks in PyPy... It really becomes a big problem after iterators heavily used in Python nowadays. Some builtin functions like zip, map, filter return iterators in Python 3 instead of lists in Python 2, means invisible bugs for code ported from Python 2, like zip(my_generator(), my_other_generator()) may leave the iterators open if exited from a for loop. Even in Python 2, functions in itertools may create these bugs. In CPython, this kind of code will work because of the ref-counting GC, so it is not obvious in CPython, but they break in PyPy.
I'm wondering since a ref-counting GC implemention is not possible for PyPy, is it possible to hack on the for loop to make it "try to" collect the generator? That may really save a lot of lives. If the generator is still referenced after the for loop, it may be the programmer's fault for not calling close(), but loop through a returned value is something different - sometimes you even do not know if it is a generator.
2016-10-21 hubo
A quick definition: A resource is something, typically represented as a program object, whose timely disposal is relevant for the correct operation of the program. Typically, it can't be closed too late (in the case of files, the process may end before all writes are flushed), but it often can't be closed too soon. This has a few obvious corallaries: * If the correct functioning of the program relies on prompt disposal, then something must be responsible for that disposal. If you place it in a container and hand that container off to a library without linearity or borrow checking, you're saying you have no interest in its disposal. * The property of "being a resource" is transitive over ownership (Hopwood's Corallary?): if X requires explicit disposal, and that disposal is managed by Y, then Y is also a resource (proof of this is easy via contradiction). Typically, there is no special magic for Y; the documentation for Y must either describe how to dispose of it, or whatever code constructed Y must also maintain control over the lifetime of X. * Are files/sockets open for reading a resource? They typically aren't, especially on GNU and BSD derived systems. Some programs will never run out of them because they will never open enough. At the same time, given that "the correct operation of the program" is specific to the actual program and its usage, the answer isn't typically clear. The first point we should notice is that explicit lifetime management is /required/ - it's a direct consequence of the definition of resource! On 22 October 2016 at 01:20, Alex S. <alex0player@gmail.com> wrote:
That’s a good point, as it means there’s probably no safe & portable way to ensure that kind of stuff. «Trying to collect» something doesn’t really fall short of an actual collection, I believe (finding referers is hard). But I believe iterclose() defined appropriately on derived iterators would solve that?..
That now places the onus on every user of iterators to ensure that their iterators are either consumed or closed; but many of the iterators we create that are resources are then handed to library functions which just can't be relied upon to maintain resource transitivity. The second point, then, is that requiring everyone to rewrite any code they have that makes or consumes generators or iterables is probably not tractible. Even if everyone decided that would be better than fixing their use of files, you'd still stumble across library code that didn't make the effort. We might have been able to do something like this if we'd had something like (dynamic-wind) when generators first arrived in python, but it's probably beyond us now without a good helping of SA and/or ugly magic. So it really is easier to rewrite file usage than it is to rewrite iterator usage, especially if we can only detect and fix a handful of obvious cases in the runtime. -- William Leslie Notice: Likely much of this email is, by the nature of copyright, covered under copyright law. You absolutely MAY reproduce any part of it in accordance with the copyright law of the nation you are reading this in. Any attempt to DENY YOU THOSE RIGHTS would be illegal without prior contractual agreement.
On Fri, Oct 21, 2016 at 10:13:45PM +0800, hubo wrote:
Well I'm really shocked to find out what I thought was a "automatic close" is really the ref-couting GC of CPython, means that a lot of my code breaks in PyPy...
But does it really? If you've run your code in PyPy, and it obviously, clearly breaks, then why are you so shocked? You should have already known this. (Unless this is your first time running your code under PyPy.) But if your code runs under PyPy, with no crashes, no exceptions, no failures caused by running out of file descriptors... then you can't really say your code is broken. What does it matter if your application doesn't close the files until exit, if you only open three files and the application never runs for more than two seconds? I'd like to get a good idea of how often this is an actual problem, causing scripts and applications to fail when run in PyPy. Actual failures, not just wasting a file descriptor or three.
I'm wondering since a ref-counting GC implemention is not possible for PyPy, is it possible to hack on the for loop to make it "try to" collect the generator? That may really save a lot of lives.
Saving lives? That's a bit of an exaggeration, isn't it? There is a big discussion going on over on the Python-Ideas mailing list, and exaggerated, over-the-top responses aren't going to help this proposal's case. Already people have said this issue is only a problem for PyPy, so it's PyPy's problem to fix. -- Steve
Hi Steven, On 22 October 2016 at 01:13, Steven D'Aprano <steve@pearwood.info> wrote:
Saving lives? That's a bit of an exaggeration, isn't it?
There is a big discussion going on over on the Python-Ideas mailing list, and exaggerated, over-the-top responses aren't going to help this proposal's case. Already people have said this issue is only a problem for PyPy, so it's PyPy's problem to fix.
Why did CPython add ResourceWarning when a file is not explicitly closed in the first place? That's because relying on reference counting to close files has been judged a bad programming practice by python-dev. The reasons for this judgement are along the lines of "because it breaks on every non-refcounted implementation". It's a good move from PyPy's point of view, and it is something that we implemented on PyPy2 too. Now the present discussion is about a similar case. Based on past experience, people are going to say first "it's not really important", then "it works fine in CPython", and finally in a few years python-dev is going to realize that it's maybe more important than what they originally thought. Nathaniel is trying to do the right thing from the start instead, so deserves a +1. Now *how* to do it exactly is a bit unclear, and largely involves language design questions (which pypy-dev is not the best place to address). A bientôt, Armin.
Hi again, On 23 October 2016 at 01:23, Armin Rigo <armin.rigo@gmail.com> wrote:
then "it works fine in CPython"
I forgot the usual "if you don't have references from a cycle" here. That is, it works fine in CPython unless your object graph is in some shape that is hard to predict, relatively rare, and can be created internally by the implementation (or its next version). Not very sane to *rely* on that for programs to work, imho. A bientôt, Armin.
On Sun, Oct 23, 2016 at 01:23:25AM +0200, Armin Rigo wrote:
Hi Steven,
On 22 October 2016 at 01:13, Steven D'Aprano <steve@pearwood.info> wrote:
Saving lives? That's a bit of an exaggeration, isn't it?
There is a big discussion going on over on the Python-Ideas mailing list, and exaggerated, over-the-top responses aren't going to help this proposal's case. Already people have said this issue is only a problem for PyPy, so it's PyPy's problem to fix.
Why did CPython add ResourceWarning when a file is not explicitly closed in the first place? That's because relying on reference counting to close files has been judged a bad programming practice by python-dev. The reasons for this judgement are along the lines of "because it breaks on every non-refcounted implementation". It's a good move from PyPy's point of view, and it is something that we implemented on PyPy2 too.
Now the present discussion is about a similar case. Based on past experience, people are going to say first "it's not really important", then "it works fine in CPython", and finally in a few years python-dev is going to realize that it's maybe more important than what they originally thought.
You are probably correct. On the Python-Ideas list, I've changed from mild opposition to mild support. I don't think it will be as disruptive as some people fear, and I think it probably will of benefit. But it would be nice to have some good, concrete examples of how it will help, rather than exaggerated claims of everyone's code being broken and saving lives. (I'm sure Hubo didn't *literally* mean people die from this, but still, it is hard to take people seriously when they exaggerate in this way unless they are clearly doing it in fun.)
Nathaniel is trying to do the right thing from the start instead, so deserves a +1.
I agree. -- Steve
Well the code don't appear to be broken, in fact they have been working well for serveral months with PyPy, it only means the design is broken - there are chances that iterators are not closed correctly in certain circumstances which leads to unpredictable behaviors. It might not be critical for small scripts but may be quite critical for services that must keep running for a long time, which is what PyPy is for. Files may not be the most critical problem, the real problem is LOCK - when you use with on a lock, there are chances that it never unlocks. As far as I know quite a lot of softwares use generators on network programmings because it is convenient to process the callbacks with generators, and it is not so unusual to "call" another generator method or recurse on itself. When the connection is suddenly shutdown, the connection manager closes the generator - but not the generators called inside. Python 3 is using this as the standard programming model of asyncio, it may also suffer but not that much, because yield from seems to close the iterator automatically because it reraises the exception inside. 2016-10-24 hubo 发件人:Steven D'Aprano <steve@pearwood.info> 发送时间:2016-10-22 07:13 主题:Re: [pypy-dev] RFC: draft idea for making for loops automatically close iterators 收件人:"pypy-dev"<pypy-dev@python.org> 抄送: On Fri, Oct 21, 2016 at 10:13:45PM +0800, hubo wrote:
Well I'm really shocked to find out what I thought was a "automatic close" is really the ref-couting GC of CPython, means that a lot of my code breaks in PyPy...
But does it really? If you've run your code in PyPy, and it obviously, clearly breaks, then why are you so shocked? You should have already known this. (Unless this is your first time running your code under PyPy.) But if your code runs under PyPy, with no crashes, no exceptions, no failures caused by running out of file descriptors... then you can't really say your code is broken. What does it matter if your application doesn't close the files until exit, if you only open three files and the application never runs for more than two seconds? I'd like to get a good idea of how often this is an actual problem, causing scripts and applications to fail when run in PyPy. Actual failures, not just wasting a file descriptor or three.
I'm wondering since a ref-counting GC implemention is not possible for PyPy, is it possible to hack on the for loop to make it "try to" collect the generator? That may really save a lot of lives.
Saving lives? That's a bit of an exaggeration, isn't it? There is a big discussion going on over on the Python-Ideas mailing list, and exaggerated, over-the-top responses aren't going to help this proposal's case. Already people have said this issue is only a problem for PyPy, so it's PyPy's problem to fix. -- Steve _______________________________________________ pypy-dev mailing list pypy-dev@python.org https://mail.python.org/mailman/listinfo/pypy-dev
Hi, On 24 October 2016 at 06:03, hubo <hubo@jiedaibao.com> wrote:
long time, which is what PyPy is for. Files may not be the most critical problem, the real problem is LOCK - when you use with on a lock, there are chances that it never unlocks.
Bah. I would say that it makes the whole "with" statement pointless in case you're using "await" somewhere inside it. In my own honest opinion it should not be permitted to do that at all---like, it should be a SyntaxError to use "await" inside a "with". (This opinion is based on a very vague understanding of async/await in the first place, so please take it with a grain of salt.) Just my 2 cents. Armin
On Sun, Oct 23, 2016 at 11:42 PM, Armin Rigo <armin.rigo@gmail.com> wrote:
Hi,
On 24 October 2016 at 06:03, hubo <hubo@jiedaibao.com> wrote:
long time, which is what PyPy is for. Files may not be the most critical problem, the real problem is LOCK - when you use with on a lock, there are chances that it never unlocks.
Bah. I would say that it makes the whole "with" statement pointless in case you're using "await" somewhere inside it. In my own honest opinion it should not be permitted to do that at all---like, it should be a SyntaxError to use "await" inside a "with". (This opinion is based on a very vague understanding of async/await in the first place, so please take it with a grain of salt.)
Interesting historical tidbit: until PEP 342, it actually was a syntax error to yield inside try, or at least inside try/finally. Actually relevant modern answer: 'await' inside 'with' turns out to be OK, because async functions are always run by some supervisor (like an event loop), and so the solution is that the supervisor guarantees as part of its semantics that it will always run async functions to completion. (You're right that this isn't obvious though -- it took very confused thread on the async-sig mailing list to recognize the problem and solution.) Since event loops are already rocket science and you only need one of them, requiring them to take some care here isn't a big deal. The problem with (async) generators is that everyone who uses them also has to make some similar guarantee (i.e., they must be run to completion, either by exhausting them or calling .close()), but they get used all over the place in random user code, and it's not reasonable to ask users to figure this out every time they write a 'for' loop. -n -- Nathaniel J. Smith -- https://vorpus.org
Well, with scope and try...finally... are really important in asynchronized programming, so I'm afraid we cannot just drop it. There isn't a replacement for an iterator to safely dispose some resources; and now we know that EVEN with and finally are not really safe... It turns out that what we love most and trust most harm us most :( 2016-10-24 hubo 发件人:Nathaniel Smith <njs@pobox.com> 发送时间:2016-10-24 15:24 主题:Re: [pypy-dev] RFC: draft idea for making for loops automatically close iterators 收件人:"Armin Rigo"<armin.rigo@gmail.com> 抄送:"hubo"<hubo@jiedaibao.com>,"PyPy Developer Mailing List"<pypy-dev@python.org> On Sun, Oct 23, 2016 at 11:42 PM, Armin Rigo <armin.rigo@gmail.com> wrote:
Hi,
On 24 October 2016 at 06:03, hubo <hubo@jiedaibao.com> wrote:
long time, which is what PyPy is for. Files may not be the most critical problem, the real problem is LOCK - when you use with on a lock, there are chances that it never unlocks.
Bah. I would say that it makes the whole "with" statement pointless in case you're using "await" somewhere inside it. In my own honest opinion it should not be permitted to do that at all---like, it should be a SyntaxError to use "await" inside a "with". (This opinion is based on a very vague understanding of async/await in the first place, so please take it with a grain of salt.)
Interesting historical tidbit: until PEP 342, it actually was a syntax error to yield inside try, or at least inside try/finally. Actually relevant modern answer: 'await' inside 'with' turns out to be OK, because async functions are always run by some supervisor (like an event loop), and so the solution is that the supervisor guarantees as part of its semantics that it will always run async functions to completion. (You're right that this isn't obvious though -- it took very confused thread on the async-sig mailing list to recognize the problem and solution.) Since event loops are already rocket science and you only need one of them, requiring them to take some care here isn't a big deal. The problem with (async) generators is that everyone who uses them also has to make some similar guarantee (i.e., they must be run to completion, either by exhausting them or calling .close()), but they get used all over the place in random user code, and it's not reasonable to ask users to figure this out every time they write a 'for' loop. -n -- Nathaniel J. Smith -- https://vorpus.org
*shrug* The real solution to relying on implementation-defined behaviour isn't to emulate that behavour through even more pervasive user-visible (and user-breakable) generator madness, but to use static analysis tools as part of the development process that can detect and report common violations of linearity (especially *local* violations, which seem to be very common and easy to detect). Do any of the existing tools do such checks? On 22 October 2016 at 01:13, hubo <hubo@jiedaibao.com> wrote:
Well I'm really shocked to find out what I thought was a "automatic close" is really the ref-couting GC of CPython, means that a lot of my code breaks in PyPy... It really becomes a big problem after iterators heavily used in Python nowadays. Some builtin functions like zip, map, filter return iterators in Python 3 instead of lists in Python 2, means invisible bugs for code ported from Python 2, like zip(my_generator(), my_other_generator()) may leave the iterators open if exited from a for loop. Even in Python 2, functions in itertools may create these bugs. In CPython, this kind of code will work because of the ref-counting GC, so it is not obvious in CPython, but they break in PyPy.
I'm wondering since a ref-counting GC implemention is not possible for PyPy, is it possible to hack on the for loop to make it "try to" collect the generator? That may really save a lot of lives. If the generator is still referenced after the for loop, it may be the programmer's fault for not calling close(), but loop through a returned value is something different - sometimes you even do not know if it is a generator.
2016-10-21 ________________________________ hubo ________________________________
发件人:Armin Rigo <armin.rigo@gmail.com> 发送时间:2016-10-18 16:01 主题:Re: [pypy-dev] RFC: draft idea for making for loops automatically close iterators 收件人:"Nathaniel Smith"<njs@pobox.com> 抄送:"PyPy Developer Mailing List"<pypy-dev@python.org>
Hi,
On 17 October 2016 at 10:08, Nathaniel Smith <njs@pobox.com> wrote:
thought I'd send around a draft to see what you think. (E.g., would this be something that makes your life easier?)
As a general rule, PyPy's GC behavior is similar to CPython's if we tweak the program to start a chain of references at a self-referential object. So for example, consider that the outermost loop of a program takes the objects like the async generators, and stores them inside such an object:
class A: def __init__(self, ref): self.ref = ref self.myself = self
and then immediately forget that A instance. Then both this A instance and everything it refers to is kept alive until the next cyclic GC occurs. PyPy just always exhibits that behavior instead of only when you start with reference cycles.
So the real issue should not be "how to so something that will make PyPy happy", or not only---it should be "how to do something that will make CPython happy even in case of reference cycles". If you don't, then arguably CPython is slightly broken.
Yes, anything that can reduce file descriptor leaks in Python sounds good to me.
A bientôt,
Armin. _______________________________________________ pypy-dev mailing list pypy-dev@python.org https://mail.python.org/mailman/listinfo/pypy-dev
_______________________________________________ pypy-dev mailing list pypy-dev@python.org https://mail.python.org/mailman/listinfo/pypy-dev
-- William Leslie Notice: Likely much of this email is, by the nature of copyright, covered under copyright law. You absolutely MAY reproduce any part of it in accordance with the copyright law of the nation you are reading this in. Any attempt to DENY YOU THOSE RIGHTS would be illegal without prior contractual agreement.
participants (7)
-
Alex S. -
Armin Rigo -
hubo -
Nathaniel Smith -
Oscar Benjamin -
Steven D'Aprano -
William ML Leslie