[Chicago] Popen/subprocess/Poll Question.

Daniel Griffin dgriff1 at gmail.com
Thu Jul 22 17:07:21 CEST 2010


Using communicate is basically going to block until the process is done. You
can use a tempfile and read the temp file at some interval to print to
screen, then fseek(0) at the end to get the contents of the file. Also, if
you use PIPE it goes to memory which can really screw you up if you have a
lot of output.

To use poll you basically have to check it periodically. I do this in
twisted with a reactor.callLater(1, blah.check_process()) . You can do the
same with a while loop with a sleep in it.

Dan



On Thu, Jul 22, 2010 at 9:55 AM, Samir Faci <samir at esamir.com> wrote:

> I'm sure this is something fairly silly I'm overlooking, but I've been
> spending too much time on this... so I figured I should ask the powers
> that be.
>
> I'm running a process that's usually takes a while.... (in the best
> case scenario a few hours... going on days occasionally)
>
> I'm presuming I need to spawn a thread for the process so it doesn't block.
>
> So I have something like this:
>
> class testit(Thread):
>   def __init__ (self,cmd):
>      Thread.__init__(self)
>      self.cmd = cmd
>      self.p = sub.Popen(shlex.split(self.cmd), stdout=sub.PIPE,
> stderr=sub.PIPE)
>   def run(self):
>      out,err = self.p.communicate()
>      print out
>      print err
>   def getProcess(self):
>       return self.p
>
> current = testit("watch -n 5 date")
> proc = current.getProcess
>
> #### insert useful code here
>
> current.start()
> current.join()
>
>
> Now, what I'm trying to accomplish is to have it print its output to a
> screen AND let me capture its output.  I'd like to see the output.
> I'd also like to see the output as its processing, not capture it, and
> print it to screen and then write to file.
>
> I think I need to poll it before starting the process, and register an
> event so whenever stdout changes, I can pull that information and push
> write to file.
> Though the whole events/polling isn't making much sense to me right
> now.  Any hints?
>
>
> --
> Samir Faci
> *insert title*
> fortune | cowsay -f /usr/share/cows/tux.cow
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20100722/cce5a195/attachment.html>


More information about the Chicago mailing list