How to send a var to stdin of an external software

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Benjamin Watine

    How to send a var to stdin of an external software

    Hi the list,

    I need to send a var to stdin of an external soft ("cat" command for
    example).

    How can I do this ? I would like a function like that :

    theFunction ('cat -', stdin=myVar)

    I don't need to get any return value.

    Another related question : Is there's a limitation of var size ? I would
    have var up to 10 MB.

    Thanks !

    Ben

  • Marko Rauhamaa

    #2
    Re: How to send a var to stdin of an external software

    Benjamin Watine <watine@cines.f r>:
    How can I do this ? I would like a function like that :
    >
    theFunction ('cat -', stdin=myVar)
    >
    Another related question : Is there's a limitation of var size ? I
    would have var up to 10 MB.
    import subprocess
    myVar = '*' * 10000000
    cat = subprocess.Pope n('cat',shell = True,stdin = subprocess.PIPE )
    cat.stdin.write (myVar)
    cat.stdin.close ()
    cat.wait()


    Marko

    --
    Marko Rauhamaa mailto:marko@pa cujo.net http://pacujo.net/marko/

    Comment

    • Sion Arrowsmith

      #3
      Re: How to send a var to stdin of an external software

      Benjamin Watine <watine@cines.f rwrote:
      >How can I do this ? I would like a function like that :
      >
      > theFunction ('cat -', stdin=myVar)
      >
      >I don't need to get any return value.
      http://docs.python.org/lib/node534.html says this is spelt

      myVar = subprocess.Pope n(["cat", "-"], stdout=subproce ss.PIPE).commun icate()[0]

      (Probably not obvious how to find this if you've not come across the
      backtick notation in shell or Perl.)

      --
      \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
      "Frankly I have no feelings towards penguins one way or the other"
      -- Arthur C. Clarke
      her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

      Comment

      • Benjamin Watine

        #4
        Re: How to send a var to stdin of an external software

        Marko Rauhamaa a écrit :
        Benjamin Watine <watine@cines.f r>:
        >
        >How can I do this ? I would like a function like that :
        >>
        > theFunction ('cat -', stdin=myVar)
        >>
        >Another related question : Is there's a limitation of var size ? I
        >would have var up to 10 MB.
        >
        import subprocess
        myVar = '*' * 10000000
        cat = subprocess.Pope n('cat',shell = True,stdin = subprocess.PIPE )
        cat.stdin.write (myVar)
        cat.stdin.close ()
        cat.wait()
        >
        >
        Marko
        >
        Thank you Marko, it's exactly what I need.

        And if somebody need it : to get the stdout in a var (myNewVar), not in
        the shell :

        cat = subprocess.Pope n('cat', shell = True, stdin = subprocess.PIPE ,
        stdout=subproce ss.PIPE)
        cat.stdin.write (myVar)
        cat.stdin.close ()
        cat.wait()
        myNewVar = cat.stdout.read ()

        Is it correct ?

        Ben

        Comment

        • Bryan Olson

          #5
          Re: How to send a var to stdin of an external software

          Benjamin Watine wrote:
          And if somebody need it : to get the stdout in a var (myNewVar), not in
          the shell :
          >
          cat = subprocess.Pope n('cat', shell = True, stdin = subprocess.PIPE ,
          stdout=subproce ss.PIPE)
          cat.stdin.write (myVar)
          cat.stdin.close ()
          cat.wait()
          myNewVar = cat.stdout.read ()
          >
          Is it correct ?
          No, not really. It is prone to deadlock. The external program might
          work by iteratively reading a little input and writing a little
          output, as 'cat' almost surely does. If the size of myVar exceeds
          the buffer space in cat and the pipes, you get stuck.

          Your Python program can block at "cat.stdin.writ e(myVar)", waiting
          for cat to read from its input pipe, while cat blocks at a write
          to its output stream, waiting for you to start reading and freeing
          up buffer space. Pipe loops are tricky business.

          Popular solutions are to make either the input or output stream
          a disk file, or to create another thread (or process) to be an
          active reader or writer.


          --
          --Bryan

          Comment

          • Bryan Olson

            #6
            Re: How to send a var to stdin of an external software

            I wrote:
            [...] Pipe loops are tricky business.
            >
            Popular solutions are to make either the input or output stream
            a disk file, or to create another thread (or process) to be an
            active reader or writer.
            Or asynchronous I/O. On Unix-like systems, you can select() on
            the underlying file descriptors. (MS-Windows async mechanisms are
            not as well exposed by the Python standard library.)


            --
            --Bryan

            Comment

            • Benjamin Watine

              #7
              Re: How to send a var to stdin of an external software

              Bryan Olson a écrit :
              I wrote:
              >[...] Pipe loops are tricky business.
              >>
              >Popular solutions are to make either the input or output stream
              >a disk file, or to create another thread (or process) to be an
              >active reader or writer.
              >
              Or asynchronous I/O. On Unix-like systems, you can select() on
              the underlying file descriptors. (MS-Windows async mechanisms are
              not as well exposed by the Python standard library.)
              >
              Hi Bryan

              Thank you so much for your advice. You're right, I just made a test with
              a 10 MB input stream, and it hangs exactly like you said (on
              cat.stdin.write (myStdin))...

              I don't want to use disk files. In reality, this script was previously
              done in bash using disk files, but I had problems with that solution
              (the files wasn't always cleared, and sometimes, I've found a part of
              previous input at the end of the next input.)

              That's why I want to use python, just to not use disk files.

              Could you give me more information / examples about the two solutions
              you've proposed (thread or asynchronous I/O) ?

              Thank you !

              Ben

              Comment

              • Floris Bruynooghe

                #8
                Re: How to send a var to stdin of an external software

                On Mar 14, 11:37 am, Benjamin Watine <wat...@cines.f rwrote:
                Bryan Olson a écrit :
                >
                I wrote:
                [...] Pipe loops are tricky business.
                >
                Popular solutions are to make either the input or output stream
                a disk file, or to create another thread (or process) to be an
                active reader or writer.
                >
                Or asynchronous I/O. On Unix-like systems, you can select() on
                the underlying file descriptors. (MS-Windows async mechanisms are
                not as well exposed by the Python standard library.)
                >
                Hi Bryan
                >
                Thank you so much for your advice. You're right, I just made a test with
                a 10 MB input stream, and it hangs exactly like you said (on
                cat.stdin.write (myStdin))...
                >
                I don't want to use disk files. In reality, this script was previously
                done in bash using disk files, but I had problems with that solution
                (the files wasn't always cleared, and sometimes, I've found a part of
                previous input at the end of the next input.)
                >
                That's why I want to use python, just to not use disk files.
                >
                Could you give me more information / examples about the two solutions
                you've proposed (thread or asynchronous I/O) ?
                The source code of the subprocess module shows how to do it with
                select IIRC. Look at the implementation of the communicate() method.

                Regards
                Floris

                Comment

                Working...