1

I need to run a python script at the time a sh file is called and during all the time this process is running.

basically it is a python spinner during installation

import sys
import time

do
  def spinn():
  print "processing...\\",
  syms = ['\\', '|', '/', '-']
  bs = '\b'
  for _ in range(10):
      for sym in syms:
          sys.stdout.write("\b%s" % sym)
          sys.stdout.flush()
          time.sleep(.1)
  spinn()
while
  def installing():
    import subprocess
    subprocess.call(["sudo sh", "installer.sh"],shell=True)
  installing()

is there a way to to this on python?

4

1 回答 1

1

subprocess.call()等待子进程退出。改为使用subprocess.Popen。然后.poll()定期使用以检查进程何时退出。

import itertools
import os
import subprocess
import sys
import time

def installing():
    null = open(os.devnull, 'wb')
    p = subprocess.Popen('echo blah && sleep 5', shell=True, stdout=null)
    #p = subprocess.Popen('sudo sh installer.sh', shell=True, stdout=null)
    return p, null

def spin(p_stdout):
    p, stdout = p_stdout
    syms = itertools.cycle(['\\', '|', '/', '-'])
    sys.stdout.write('processing....')
    sys.stdout.flush()
    while p.poll() is None:
        sys.stdout.write('\b'+next(syms))
        sys.stdout.flush()
        time.sleep(0.1)
    p.wait()
    stdout.close()

spin(installing())
于 2013-06-25T15:21:09.607 回答