home Mail List
Info
Info
Meetings
Goals
Upcoming
Projects
FAQ
Security
Links

[Date Prev][Date Next] [Chronological] [Thread] [Top]

[NMLUG] Python tight loop causing massive CPU barfage



import stat

I just e-mailed another example a minute ago that contains 
a complete little program.

On Wed, 09 Feb 2005 15:07:11 -0700
  Paul Tietjens <paul.tietjens@moriarty.k12.nm.us> wrote:
> Peter Espen wrote:
> 
>>
>> I used the following python code to open 175,000 mail 
>>files, read the 
>> first 1024 lines from each file and search for a line 
>>beginning with 
>> "Subject" from each.
>>
>> It took 2 minutes 24 seconds on a 800MHz Pentium III 
>>running SuSE 9.0:
>>
>> real    2m24.097s
>> user    2m10.270s
>> sys     0m9.530s
>>
>>
>> Here's the read/search loop for each file:
>>
>>     myre = re.compile('^Subject')
>>
>>     for fn in filelist:
>>
>>        mode = os.stat(fn)[stat.ST_MODE]
>>
>>        if stat.S_ISREG(mode):
>>
>>            mf_fd = os.open( fn, os.O_RDONLY )
>>            the_file = os.fdopen(mf_fd, "r", 1024)
>>            the_lines = the_file.readlines(1024)
>>            for line in the_lines:
>>                m = myre.match(line)
>>            the_file.close()
>>
> 
> Wow!  Do you have the rest of that?  I'm not sure what 
>module I need for 'stat' to work, but after import 'os' I 
>stil l get:
> 
> Traceback (most recent call last):
>  File "./foo.py", line 10, in ?
>    mode = os.stat(fn)[stat.ST_MODE]
> NameError: name 'stat' is not defined
> 
> 
> _______________________________________________
> NMLUG mailing list
> NMLUG@nmlug.org
> http://www.nmlug.org/mailman/listinfo/nmlug



Please send sugestions and comments to webmaster@nmlug.org.
Valid XHTML 1.1! Valid CSS! Powered by Debian Powered by Apache