1- """
2- $ ipcluster start -n 4
1+ """ Before this code is run in a given session, an ipython cluster session needs
2+ to be started. This can be done with 4 engines using the ipcluter command:
3+
4+ $ ipcluster start -n 4
5+
6+ More engines can be added on the fly by starting another 1
7+
8+ $ ipengine
39"""
410from IPython .parallel import Client
11+ import time
12+
13+ # This assumes that the client will connect to the default profile. If not, a
14+ # profile or a json file can be provided to the client at creation.
15+ c = Client ()
16+ print "The client contains 4 different engines with different IDs" , c .ids
17+
18+ # 2 different kinds of view on the engines:
19+ direct_view_engine_0 = c [0 ]
20+ load_balanced_view = c .load_balanced_view ()
21+
22+ # Let's define what we want our engines to do for us
23+ mul = lambda x ,y : x * y
24+ direct_view_engine_0 .apply (mul , 4 , 5 )
25+
26+ # The loadbalanced view can receive a map_sync or map_async with a callable and
27+ # a list of arguments.
28+ load_balanced_view .map_sync (mul , range (5 ), range (10 , 15 ))
29+
30+ # c[:] is a (multiplexer) direct view on all 4 engines. It can apply a target
31+ # function to all engines. This version is asynchronous: the result will be
32+ # stored separately for each engine.
33+ c [:].apply_async (mul , 4 , 5 ).get_dict ()
534
6- c = Client ()
735
8- print c .ids
9- c [:].apply_sync (lambda : "Hello, World" )
36+ def test_latency (view , nb_tasks ):
37+ """ Measure the latency of a given direct view on an engine for a given
38+ setup where nb_tasks tasks are sent to it.
39+ """
40+ tic = time .time ()
41+ echo = lambda x : x
42+ tic = time .time ()
43+ for i in xrange (nb_tasks ):
44+ view .apply_async (echo , '' )
45+ toc = time .time ()
46+ view .wait ()
47+ tac = time .time ()
48+ sent = toc - tic
49+ roundtrip = tac - tic
50+ return sent , roundtrip
51+
52+ # test overhead related to
53+ for client in c :
54+ print
0 commit comments