O'Reilly logo

A Concise Introduction to Programming in Python by Mark J. Johnson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 27

Parallel Programming

Real-world problems often take too long to solve on a single processor because of their size or computational demands. Parallel processing is a family of mechanisms for coordinating multiple tasks that work in parallel to solve a single problem. In this chapter, we explore the use of parallel programming with the example from Chapter 10.

Listing 27.1: Monte Carlo Integration (Parallel Version)

 1 # montecarlo.py
 2
 3 from random import uniform
 4 from math import exp
 5 import multiprocessing
 6
 7 def count_hits(f, a, b, m, n):
 8 hits = 0
 9 for i in range(n):
10  x = uniform(a, b)
11  y = uniform(0, m)
12  if y <= f(x):
13    hits += 1
14 return hits
15
16 def estimate_area_mp(f, a, b, m, n=1000):
17 workers = multiprocessing.cpu_count() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required