Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I agree with @aix, <code>multiprocessing</code> is definitely the way to go. Regardless you will be i/o bound -- you can only read so fast, no matter how many parallel processes you have running. But there can easily be <em>some</em> speedup. </p> <p>Consider the following (input/ is a directory that contains several .txt files from Project Gutenberg).</p> <pre><code>import os.path from multiprocessing import Pool import sys import time def process_file(name): ''' Process one file: count number of lines and words ''' linecount=0 wordcount=0 with open(name, 'r') as inp: for line in inp: linecount+=1 wordcount+=len(line.split(' ')) return name, linecount, wordcount def process_files_parallel(arg, dirname, names): ''' Process each file in parallel via Poll.map() ''' pool=Pool() results=pool.map(process_file, [os.path.join(dirname, name) for name in names]) def process_files(arg, dirname, names): ''' Process each file in via map() ''' results=map(process_file, [os.path.join(dirname, name) for name in names]) if __name__ == '__main__': start=time.time() os.path.walk('input/', process_files, None) print "process_files()", time.time()-start start=time.time() os.path.walk('input/', process_files_parallel, None) print "process_files_parallel()", time.time()-start </code></pre> <p>When I run this on my dual core machine there is a noticeable (but not 2x) speedup:</p> <pre><code>$ python process_files.py process_files() 1.71218085289 process_files_parallel() 1.28905105591 </code></pre> <p>If the files are small enough to fit in memory, and you have lots of processing to be done that isn't i/o bound, then you should see even better improvement.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload