<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import heapq <EOL> from ipyparallel . error import RemoteError <EOL> def mergesort ( list_of_lists , key = None ) : <EOL> """<STR_LIT>""" <EOL> heap = [ ] <EOL> for i , itr in enumerate ( iter ( pl ) for pl in list_of_lists ) : <EOL> try : <EOL> item = next ( itr ) <EOL> if key : <EOL> toadd = ( key ( item ) , i , item , itr ) <EOL> else : <EOL> toadd = ( item , i , itr ) <EOL> heap . append ( toadd ) <EOL> except StopIteration : <EOL> pass <EOL> heapq . heapify ( heap ) <EOL> if key : <EOL> while heap : <EOL> _ , idx , item , itr = heap [ <NUM_LIT:0> ] <EOL> yield item <EOL> try : <EOL> item = next ( itr ) <EOL> heapq . heapreplace ( heap , ( key ( item ) , idx , item , itr ) ) <EOL> except StopIteration : <EOL> heapq . heappop ( heap ) <EOL> else : <EOL> while heap : <EOL> item , idx , itr = heap [ <NUM_LIT:0> ] <EOL> yield item <EOL> try : <EOL> heapq . heapreplace ( heap , ( next ( itr ) , idx , itr ) ) <EOL> except StopIteration : <EOL> heapq . heappop ( heap ) <EOL> def remote_iterator ( view , name ) : <EOL> """<STR_LIT>""" <EOL> view . execute ( '<STR_LIT>' % ( name , name ) , block = True ) <EOL> while True : <EOL> try : <EOL> result = view . apply_sync ( next , Reference ( '<STR_LIT>' + name ) ) <EOL> except RemoteError as e : <EOL> if e . ename == '<STR_LIT>' : <EOL> raise StopIteration <EOL> else : <EOL> raise e <EOL> else : <EOL> yield result <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from ipyparallel import Client , Reference <EOL> rc = Client ( ) <EOL> view = rc [ : ] <EOL> print ( '<STR_LIT>' , rc . ids ) <EOL> a0 = range ( <NUM_LIT:5> , <NUM_LIT:20> ) <EOL> a1 = range ( <NUM_LIT:10> ) <EOL> a2 = range ( <NUM_LIT:15> , <NUM_LIT> ) <EOL> rc [ <NUM_LIT:0> ] [ '<STR_LIT:a>' ] = a0 <EOL> rc [ <NUM_LIT:1> ] [ '<STR_LIT:a>' ] = a1 <EOL> rc [ <NUM_LIT:2> ] [ '<STR_LIT:a>' ] = a2 <EOL> aa0 = remote_iterator ( rc [ <NUM_LIT:0> ] , '<STR_LIT:a>' ) <EOL> aa1 = remote_iterator ( rc [ <NUM_LIT:1> ] , '<STR_LIT:a>' ) <EOL> aa2 = remote_iterator ( rc [ <NUM_LIT:2> ] , '<STR_LIT:a>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( list ( mergesort ( [ a0 , a1 , a2 ] ) ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( list ( mergesort ( [ aa0 , aa1 , aa2 ] ) ) ) </s>
