process and spinning slash

Alex Martelli aleaxit at yahoo.com
Sun Oct 30 11:03:52 EST 2005


benz <benzwt at yahoo.com.tw> wrote:
   ...
> def spin(delay):
> 
>   pattern=['-','\\','|','/','-','\\','|']
> 
>   while 1:
>     for i in pattern:
>       sys.stdout.write(i + " ")
>       sys.stdout.flush()
>       sys.stdout.write("\b\b")
>       time.sleep(delay)
> 
> pid = os.fork()
> 
> if pid == 0:
>   os.execvp("du",("du","-shc","/"))
> else:
>   spin(0.05)
> 
> 
> However, the spinner is kept spinning even the child process was ended.

It would be astonishing if it were otherwise!  The loop in function spin
is deliberately coded to NEVER terminate, so of course it never does.

> Any idea ? Thanks!

Have the spin function accept the pid argument and exit the loop if said
pid has terminated; to check the latter, e.g., os.kill(pid, 0) -- this
will raise an OSError if no process with that pid exists, so you can use
a try/except OSError: to catch that and break as appropriate.


Alex



More information about the Python-list mailing list