Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide shortcut for aprint? #31

Closed
yuxincs opened this issue Oct 16, 2018 · 1 comment
Closed

Provide shortcut for aprint? #31

yuxincs opened this issue Oct 16, 2018 · 1 comment

Comments

@yuxincs
Copy link
Contributor

yuxincs commented Oct 16, 2018

Currently the print function doesn't work well with async code. see at https://gist.github.com/nathan-hoad/8966377#gistcomment-1222842

If you feed a few hundred bytes into print function, you will see BlockingIOError.

It is very easy to solve this problem in aioconsole IMO

A sample code could be:

from aioconsole.stream import get_standard_streams

async def aprint(string):
    _, writer = get_standard_streams()
    writer.write(string.encode('utf-8'))
    writer.write('\n'.encode('utf-8'))
    await writer.drain()

Perhaps provide a shortcut for aprint might be easy for developers since we have ainput in the package.

@vxgmichel
Copy link
Owner

Hi @RyanWangGit and thanks for your report!

For the reference, the issue can be reproduced with the following code:

import asyncio
from aioconsole import get_standard_streams

async def main():
    _, writer = await get_standard_streams()
    print('a'*64*1024)
    # print('a'*64*1024, file=writer)
    # print('OK', file=writer)
    # await writer.drain()

if __name__ == '__main__':
    asyncio.run(main())

It produces the following error:

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    asyncio.run(main())
  File "/home/vimiche/miniconda/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/home/vimiche/miniconda/lib/python3.7/asyncio/base_events.py", line 568, in run_until_complete
    return future.result()
  File "test.py", line 6, in main
    print('a'*64*1024)
BlockingIOError: [Errno 11] write could not complete without blocking

While investigating this, I also realized there was an issue with the handling of linux pipes (see #32).

Perhaps provide a shortcut for aprint might be easy for developers since we have ainput in the package.

Sounds good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants