On 9/8/2018 7:17 AM, Jonathan Fine wrote:
I thank Steve D'Aprano for pointing me to this real-life (although
perhaps extreme) code example
https://github.com/Tinche/aiofiles/blob/master/aiofiles/threadpool/__init__.py#L17-L37
<code>
def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None,
closefd=True, opener=None, *, loop=None, executor=None):
return AiofilesContextManager(_open(file, mode=mode, buffering=buffering,
encoding=encoding, errors=errors,
newline=newline, closefd=closefd,
opener=opener, loop=loop,
executor=executor))
Given that open and _open, likely written at the same time, have the
same signature, I would have written the above as the slightly faster call
return AiofilesContextManager(_open(
file, mode, buffering, encoding, errors, newline,
closefd, opener, loop=loop, executor=executor))
@asyncio.coroutine
def _open(file, mode='r', buffering=-1, encoding=None, errors=None,
newline=None,
closefd=True, opener=None, *, loop=None, executor=None):
"""Open an asyncio file."""
if loop is None:
loop = asyncio.get_event_loop()
cb = partial(sync_open, file, mode=mode, buffering=buffering,
encoding=encoding, errors=errors, newline=newline,
closefd=closefd, opener=opener)
f = yield from loop.run_in_executor(executor, cb)
return wrap(f, loop=loop, executor=executor)
</code>
--
Terry Jan Reedy
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/