Skip to content

asyncio: patch_stdout and initialization errors #1213

Open
@carlescufi

Description

@carlescufi

Any strings printed before executing await session.prompt_async() don't seem to appear on the console. Is the recommended approach to perform (synchronous) initialization prior to patching stdout?

Below is a simple example, where the Unable to open serial port string is not printed at all (tested on Linux and macOS).

[...]
async def interactive_shell():                                                  
                                                                                
    try:                                                                        
        ser = serial.Serial('/dev/thisportdoesntexist')       
    except SerialError as e:                                     
        print(f"Unable to open serial port: {e.errno}")                            
        return                                                                     
                                                                                   
    # Create Prompt.                                                               
    session = PromptSession(">> ")                                                 
    # Run echo loop. Read text from stdin, and reply it back.                      
    while True:                                                                    
        try:                                                                       
            text = await session.prompt_async()                                    
            print('You said: "{0}"'.format(text))                                  
                                                                                   
            if text == 'q' or text == 'quit':                                      
                break                                                              
                                                                                   
        except (EOFError, KeyboardInterrupt):                                      
            return                                                                 
                                                                                
async def _main():                                                              
    with patch_stdout():                                                        
        try:                                                                    
            await interactive_shell()                                           
        finally:                                                                
            print("caught")                                                     
        print("Quitting event loop. Bye.")                                      
                                                                                
                                                                                
def main():                                                                     
    asyncio.run(_main())               

Metadata

Metadata

Assignees

No one assigned

    Labels

    patch-stdoutpatch_stdout related issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions