0

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?

1
  • 2
    please have a look at this tutorial on multiprocessing Commented Aug 7, 2015 at 21:43

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

2 Comments

it also works with a simplified lambda, without passing any arguments to it: p1=multiprocessing.Process(target=lambda: h.omega())
Absolutely, good point. I made it obvious how to pass in arguments in case the example in the question was trivialized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.