Skip to content

Instantly share code, notes, and snippets.

@89465127
Created April 26, 2013 19:03
Show Gist options
  • Save 89465127/5469584 to your computer and use it in GitHub Desktop.
Save 89465127/5469584 to your computer and use it in GitHub Desktop.
Multiprocessing example, iterating over each line in a file.
import multiprocessing
import StringIO
# function to be run on each line of file
def f(x):
print x,
# Create mock file
mock_file = StringIO.StringIO()
for i in range(20):
mock_file.write("{}\n".format(i))
mock_file.seek(0)
# run it!
pool = multiprocessing.Pool(processes=4)
pool.map(f, mock_file)
# Cleanup
mock_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment