Reputation: 21343
I would like to use the most trivial example of parallelism in python and I am failing to understand how to do this from the multiprocessing
documentation.
Here is some sample toy code that shows what I would like do.
import igraph
n = 10
g = igraph.Graph()
g.add_vertices(n)
h= igraph.Graph()
h.add_vertices(n)
[add some nodes and edges to g and h]
print "h omega is ", h.omega()
print "g alpha is", g.alpha()
I would like the last two lines to run in parallel as I have multiple cores and they take a long time to run. Each one simply outputs an integer and I don't care which order I get the results in.
Is there a simple way to do this python?
Upvotes: 0
Views: 329
Reputation: 276
Assuming that omega() and alpha() are pure functions that don't change the state of your environment, you could kick off two separate processes for each call. These processes will run in parallel.
p1=multiprocessing.Process(target=lambda x: print x.omega(), args=(h,))
p2=multiprocessing.Process(target=lambda y: print y.alpha(), args=(g,))
p1.start()
p2.start()
Upvotes: 2