Book Image

IPython Interactive Computing and Visualization Cookbook

By : Cyrille Rossant
Book Image

IPython Interactive Computing and Visualization Cookbook

By: Cyrille Rossant

Overview of this book

Table of Contents (22 chapters)
IPython Interactive Computing and Visualization Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Interacting with asynchronous parallel tasks in IPython


In this recipe, we will show how to interact with asynchronous tasks running in parallel with IPython.

Getting ready

You need to start the IPython engines (see the previous recipe). The simplest option is to launch them from the Clusters tab in the notebook dashboard. In this recipe, we use four engines.

How to do it…

  1. Let's import a few modules:

    In [1]: import time
            import sys
            from IPython import parallel
            from IPython.display import clear_output, display
            from IPython.html import widgets
  2. We create a Client:

    In [2]: rc = parallel.Client()
  3. Now, we create a load-balanced view on the IPython engines:

    In [3]: view = rc.load_balanced_view()
  4. We define a simple function for our parallel tasks:

    In [4]: def f(x):
                import time
                time.sleep(.1)
                return x*x
  5. We will run this function on 100 integer numbers in parallel:

    In [5]: numbers = list(range(100))
  6. We execute f() on our list numbers in parallel...