Hi Oscar, > I don't think PyPy is in breach of the language spec here. Python made > a decision a long time ago to shun RAII-style implicit cleanup in > favour if with-style explicit cleanup. > > The solution to this problem is to move resource management outside of > the generator functions. This is true for ordinary generators without > an event-loop etc. The example in the PEP is > > async def square_series(con, to): > async with con.transaction(): > cursor = con.cursor( > 'SELECT generate_series(0, $1) AS i', to) > async for row in cursor: > yield row['i'] ** 2 > > async for i in square_series(con, 1000): > if i == 100: > break > > The normal generator equivalent of this is: > > def square_series(con, to): > with con.transaction(): > cursor = con.cursor( > 'SELECT generate_series(0, $1) AS i', to) > for row in cursor: > yield row['i'] ** 2 > > This code is already broken: move the with statement outside to the > caller of the generator function. Exactly. I used 'async with' in the PEP to demonstrate that the cleanup mechanisms are powerful enough to handle bad code patterns. Thank you, Yury
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4