473,432 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,432 software developers and data experts.

bz2 & cpu usage

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
Jul 18 '05 #1
6 2147
Brad Tilley schrieb:
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.


Three approaches:

1) Use a thread for the I/O part (i.e. the de/compression) and another one
for the GUI.

2) Collect the files first, then handle one after the other and return to
the GUI in between.

3) Use a non-blocking scheme for the whole thing (may be the trickiest).

Stefan
Jul 18 '05 #2
Stefan Behnel wrote:
Brad Tilley schrieb:
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.

Three approaches:

1) Use a thread for the I/O part (i.e. the de/compression) and another
one for the GUI.

2) Collect the files first, then handle one after the other and return
to the GUI in between.

3) Use a non-blocking scheme for the whole thing (may be the trickiest).

Stefan


The part I wrote about the "Windows GUI" is not clear. I meant the
Windows XP Desktop. My script has no GUI. Sorry for the mix-up.

Brad
Jul 18 '05 #3
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

Jul 18 '05 #4
On Tue, 19 Oct 2004 10:08:39 -0400, Brad Tilley wrote:
The part I wrote about the "Windows GUI" is not clear. I meant the Windows
XP Desktop. My script has no GUI. Sorry for the mix-up.


Before going too deeply into programmatic solutions, I'd try your program
on a completely different machine (different manufacturer or something so
the hardware is different). Excepting maybe priority setting, which is a
good idea anyhow.

While this was par for the course for Win9x, XP really shouldn't be
behaving like that in my experience. A little jumpy, maybe, but not
"unusable". Either:

* You're running it on a machine well below the stated XP requirements
(I've jammed XP onto a Pentium 233 w/ 96MB ram, but I don't recommend that
for most people; it was basically a server for me doing one specific
task.), or
* You're taking *way* too much data at a time, filling up your RAM, and
causing excessive swapping (though you ought to notice this), or
* You've got crappy hardware and your hardware can't handle the interrupt
load from the IO and the graphics at the same time; this is typically
caused by cheap motherboards, often from Via.

If your problem is hardware, you've pretty much already lost and your only
hope is different hardware.

Otherwise, as a hack, you can decompress as a stream and use the sleep
commands to voluntarily sleep for some period of time after some amount of
time or data has gone by, but this will still cause some jumpiness.

Jul 18 '05 #5
Benjamin Niemann <b.*******@betternet.de> wrote in message news:<cl**********@online.de>...
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.


Indeed.
----------------------------------------
import win32api, win32process

win32process.SetPriorityClass(win32api.GetCurrentP rocess(),
win32process.BELOW_NORMAL_PRIORITY_CLASS)

.... Invoke the compression portion of the program ...

win32process.SetPriorityClass(win32api.GetCurrentP rocess(),
win32process.NORMAL_PRIORITY_CLASS)
----------------------------------------
win32all build 159 and earlier are missing the constant
win32process.BELOW_NORMAL_PRIORITY_CLASS; that can be fixed with:
----------------------------------------
win32process.BELOW_NORMAL_PRIORITY_CLASS = 16384
----------------------------------------
Jul 18 '05 #6
Kirk Job-Sluder wrote:
Sorry for the late post, the original scrolled off the server.
> 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


One of the issues you may be running into is memory. Under windows,
using up 90% of the CPU shouldn't affect GUI performance (much) but
swapping does. According to the bzip2 man page, the maximum block size
is 900KB so you might be running into problems reading your file 1024KB
at a time. Use the system monitor control panel to check for excessive
swapping. Bzip2 uses 8x<blocksize> memory. So with the default setting
of a 900KB block size, you are looking at 7.2M + some bookeeping memory.

Another issue is that you might be better off downloading bzip2 for
windows and letting the gnu bzip2 implementation handle file input and
output. Using a shell command here might be more efficient in spite of
spawning a new process.

A third issue is that bzip2 achieves high compression efficiency at the
expense of CPU time and memory. It might be worth considering whether
gzip might occupy the sweet spot compromise between minimal archive size
and minimal cpu usage.

Fourth, how many of those files are uncompressible? I've noticed that
bzip2 tries really hard to eek out some form of savings from
uncompressible files. A filename filter for files that should not be
compressed (png, jpg, gif, sx*) might be worth doing here.


Thanks for the tips. I installed 512MB of ECC Ram and the problem went away.
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: brian.vincent | last post by:
I'm trying to use the ssh2 functions to run a graphical app. I have no problem opening ssh2_shell and running something like "/bin/ls /tmp". However when I try to do something like:...
0
by: lebo | last post by:
I'm experimenting with using Python as a very low resource usage systems management agent. Currently the best I'm getting is about a 2.4MByte base usage (python interp only) and with application,...
19
by: Philipp Lenssen | last post by:
I don't know the English word, but I'm referring to the double-dash which is used to separate parts of a sentence. I'm using — so far. Now I saw – which is slightly shorter. Some sites use --. ...
2
by: Louise GK | last post by:
Hi there. I've written a simple program that makes a simple GET form with a text input box and displays $_GET when submitted. Using Windows Character Map, I pasted in the Cyrillic capital "Ya"...
15
by: E. Robert Tisdale | last post by:
Eats, Shoots & Leaves http://www.eatsshootsandleaves.com/ I believe that C programmers should follow the *normal" rules of punctuation -- especially in their C programs.
33
by: Merrill & Michele | last post by:
Section 2.3 of K&R enumerates the complete set of escape sequences. Q1) Is it possible to ask the following question without getting into implementation/platform talk? Q2) What on earth is...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
72
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
2
by: gundam.f0rtre55 | last post by:
Hi everybody, for a new release of our J2EE Webapplication, our customer wish to allow the usage of bookmarklets. The application must be able to register URLs with several protocol types, one of...
30
by: Alf P. Steinbach | last post by:
I once suggested in that SomeOne Else(TM) should propose a string value class that accepted literals and char pointers and so on, with possible custom deleter, and in case of literal strings just...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.