import os import sys def spawnDaemon(fun): # do the UNIX double-fork magic, see Stevens' "Advanced # Programming in the UNIX Environment" for details (ISBN 0201563177) try: pid = os.fork() if pid > 0: # parent process, return and keep running return except OSError as e: print("fork #1 failed: {0} ({1})".format(e.errno,e.strerror),file=sys.stderr) sys.exit(1) os.setsid() # do second fork try: pid = os.fork() if pid > 0: # exit from second parent print("[{0}] server running".format(pid)) sys.exit(0) except OSError as e: print("fork #2 failed: {0} ({1})".format(e.errno,e.strerror),file=sys.stderr) sys.exit(1) fun() # all done os._exit(os.EX_OK) class Progress: def __init__(self,n,i0=0): self._n=n self._i0=i0 self._i=i0 self._last="" def p(self,i): i0=self._i0 n=self._n assert i0<=i<n or n<i<=i0, (i0,i,n) percentage=Progress._p(i,n,i0) res="{0}%".format(percentage) if res!=self._last: print(res,end="\r") self._last=res def done(self): print("100%") @staticmethod def _p(i,n,i0): _1=1 if n>=i0 else -1 return 100*(i+_1-i0)//(n-i0)