Ask Your Question

Revision history [back]

The computations are done as you loop over the generator, so if you use a for loop to append to a list (rather than a list comprehension), I think this will do what you want:

sage: @parallel(2)
def GMP(n): sleep(1); return (n,4*n^3 + 2*n)

sage: gen = GMP(range(100))
sage: for t in gen:
    data += [(t[0][0][0],t[1])]
    print "{0} --> {1}".format(t[0][0][0],t[1])
....:  
1 --> (1, 6)
0 --> (0, 0)
2 --> (2, 36)
3 --> (3, 114)
4 --> (4, 264)
5 --> (5, 510)
6 --> (6, 876)
7 --> (7, 1386)
8 --> (8, 2064)
9 --> (9, 2934)
^CKilling any remaining workers...
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
...
KeyboardInterrupt: 

sage: data.sort()
sage: data
[(0, (0, 0)), (1, (1, 6)), (2, (2, 36)), (3, (3, 114)), (4, (4, 264)), (5, (5, 510)), (6, (6, 876)), (7, (7, 1386)), (8, (8, 2064)), (9, (9, 2934))]