How to use subprocess

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicolas Fleury

    #1

    How to use subprocess

    Hi,
    I want to use the subprocess module (or any standard Python module) to
    run a process:
    - which stdout and stderr can each be redirected to any file-like object
    (no fileno function).
    - can be cancelled with a threading.Event .

    My problem is that the subprocess.Pope n constructor doesn't seem to
    support file-like objects (only file objects with fileno()).

    If I use subprocess.PIPE , I have the problem that the read functions of
    of the subprocess objects stdout and stderr are blocking (and I want to
    redirect them to different file-like objects, so I definitely need
    non-blocking access). How am I supposed to do it?

    Thx and regards,
    Nicolas

    It something like that that I would need to make work (but supporting
    file-like objects):

    class CancellationExc eption(Exceptio n): pass
    class CancellableComm and:
    def __init__(self, command, stdout=nullFile , stderr=nullFile ,
    refreshDelay=0. 1, raiseIfNonZero= True):
    self.command = command
    self.stdout = stdout
    self.stderr = stderr
    self.refreshDel ay = refreshDelay
    def execute(self, cancelEvent=Non e):
    if cancelEvent is None:
    cancelEvent = threading.Event () # dummy
    exitCode = None
    cmd = subprocess.Pope n(
    self.command, stdout=self.std out, stderr=self.std err)
    while exitCode is None:
    # Wait for event to be set for a specific delay
    cancelEvent.wai t(self.refreshD elay)
    if cancelEvent.isS et():
    kill(cmd.pid) # implemented as in FAQ
    raise CancellationExc eption(
    'Process "' + self.command + '" cancelled')
    exitCode = cmd.poll()
    return exitCode
  • Dennis Lee Bieber

    #2
    Re: How to use subprocess

    On Tue, 22 Mar 2005 15:18:16 -0500, Nicolas Fleury
    <nid_oizo@yahoo .com_remove_the _> declaimed the following in
    comp.lang.pytho n:
    [color=blue]
    > Hi,
    > I want to use the subprocess module (or any standard Python module) to
    > run a process:[/color]

    Pardon the question, but which version of Python /has/ a
    "subprocess " module? Is it new with 2.4?

    I've not installed 2.4 yet as I'm not sure I can locate
    pre-built external modules for some of the stuff I currently use.

    --[color=blue]
    > =============== =============== =============== =============== == <
    > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
    > wulfraed@dm.net | Bestiaria Support Staff <
    > =============== =============== =============== =============== == <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

    Comment

    • Fredrik Lundh

      #3
      Re: How to use subprocess

      Dennis Lee Bieber wrote:
      [color=blue]
      > Pardon the question, but which version of Python /has/ a
      > "subprocess " module? Is it new with 2.4?[/color]

      yes.

      for earlier versions, you can get a pure-Python Unix implementation
      here:



      and a windows installer here:



      </F>




      Comment

      • Michele Simionato

        #4
        Re: How to use subprocess

        I am not sure how safe it is, but on Linux I have just copied
        subprocess.py
        in my 2.3 installation and it worked in all the cases I tried.

        Michele Simionato

        Comment

        • Thomas Bellman

          #5
          Re: How to use subprocess

          Nicolas Fleury <nid_oizo@yahoo .com_remove_the _> writes:
          [color=blue]
          > Hi,
          > I want to use the subprocess module (or any standard Python module) to
          > run a process:
          > - which stdout and stderr can each be redirected to any file-like object
          > (no fileno function).
          > - can be cancelled with a threading.Event .[/color]
          [color=blue]
          > My problem is that the subprocess.Pope n constructor doesn't seem to
          > support file-like objects (only file objects with fileno()).[/color]
          [color=blue]
          > If I use subprocess.PIPE , I have the problem that the read functions of
          > of the subprocess objects stdout and stderr are blocking (and I want to
          > redirect them to different file-like objects, so I definitely need
          > non-blocking access). How am I supposed to do it?[/color]

          You might take a look at my asyncproc module, available at



          Specifically the Process class. It doesn't do exactly what you
          want, but maybe you can use it as inspiration for doing it yourself.

          It requires the subprocess module, but I have successfully used
          it under Python 2.3.2 with subprocess installed locally.


          --
          Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
          "Adde parvum parvo magnus acervus erit" ! bellman @ lysator.liu.se
          (From The Mythical Man-Month) ! Make Love -- Nicht Wahr!

          Comment

          Working...