bz2 & cpu usage

Benjamin Niemann b.niemann at betternet.de
Tue Oct 19 10:15:32 EDT 2004


Under Linux/Unix you would set the processes 'nice' value to tell the system to 
give your process a lower priority. Under windows do something similar using the 
TaskManager -> Set Priority... There should some windows API call to do this 
from your program, probably involving the win32 extension.


Brad Tilley wrote:
> I'd like to keep at least 50% of the cpu free while doing bz2 file 
> compression. Currently, bz2 compression takes between 80 & 100 percent 
> of the cpu and the Windows GUI becomes almost useless. How can I lower 
> the strain on the cpu and still do compression? I'm willing for the 
> compression process to take longer.
> 
> Thanks,
> 
> Brad
> 
> def compress_file(filename):
>     path = r"C:\repository_backup"
>     print path
>     for root, dirs, files in os.walk(path):
>         for f in files:
>             if f == filename:
>                 print "Compressing", f
>                 x = file(os.path.join(root, f), 'rb')
>                 os.chdir(path)
>                 y = bz2.BZ2File(f + ".bz2", 'w')
>                 while True:
>                     data = x.read(1024000)
>                     time.sleep(0.1)
>                     if not data:
>                         break
>                     y.write(data)
>                     time.sleep(0.1)
>                 y.close()
>                 x.close()
>             else:
>                 return



More information about the Python-list mailing list