[Python] Bad File Descriptor error in python writing to a pipe

Discussão em 'Python' iniciado por Stack, Janeiro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I wrote a iTunes-replacement program to run in python, and it works well thanks to import pipes. Unfortunately, recently I receive the DeprecationWarning: 'pipes' is deprecated and slated for removal in Python 3.13

    I decided to manually write to the pipe to talk to mpg123

    mpgpipe = "/tmp/mpg123remote.cmd"
    try:
    os.mkfifo(mpgpipe)
    print("Named pipe created successfully!")
    except FileExistsError:
    print("Named pipe already exists!")
    except OSError as e:
    print(f"Named pipe creation failed: {e}")
    pipfd = os.open(mpgpipe, os.O_RDWR | os.O_NONBLOCK) # the file descriptor of the pipe


    but in the body I get an OSError: [Errno 9] Bad file descriptor here's a runnable example:

    #!/usr/bin/env python3

    import os, io

    mpgpipe = "/tmp/mpg123remote.cmd"
    try:
    os.mkfifo(mpgpipe)
    print("Named pipe created successfully!")
    except FileExistsError:
    print("Named pipe already exists!")
    except OSError as e:
    print(f"Named pipe creation failed: {e}")
    pipfd = os.open(mpgpipe, os.O_RDWR | os.O_NONBLOCK) # the file descriptor of the pipe

    playlist=['a','b','c']
    with io.FileIO(pipfd, 'wb') as f:
    f.write("echo silence".encode())
    currentlyplaying=1
    with io.FileIO(pipfd, 'wb') as f:
    s=f"echo load \"{playlist[currentlyplaying-1]}\""
    f.write(s.encode())


    what I cannot understand is that the error happens at the second call, immediately after the currentlyplaying=1 line

    Continue reading...

Compartilhe esta Página