File: map.py

package info (click to toggle)
ipython 0.13.1-2%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,752 kB
  • sloc: python: 69,537; makefile: 355; lisp: 272; sh: 80; objc: 37
file content (38 lines) | stat: -rw-r--r-- 832 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from __future__ import print_function

from IPython.parallel import *

client = Client()
view = client.load_balanced_view()

@view.remote(block=True)
def square(a):
    """return square of a number"""
    return a*a

squares = map(square, range(42))

# but that blocked between each result; not exactly useful

square.block = False

arlist = map(square, range(42))
# submitted very fast

# wait for the results:
squares2 = [ r.get() for r in arlist ]

# now the more convenient @parallel decorator, which has a map method:
view2 = client[:]
@view2.parallel(block=False)
def psquare(a):
    """return square of a number"""
    return a*a

# this chunks the data into n-negines jobs, not 42 jobs:
ar = psquare.map(range(42))

# wait for the results to be done:
squares3 = ar.get()
print(squares == squares2, squares3==squares)
# True