Overview

A crazy idea inspired by this post. I wrote a function decorator that spawns a WSGI application serving a dynamic Javascript file. Load the Javascript file and your browser will start calculating the function. This way we can implement a simple map/reduce framework that offloads the calculation. Here's an example:

from parallejax import Map, Reduce

@Map('0.0.0.0', 8080)
def isprime(n):
    if n < 2:
        return False
    if n == 2:
        return True
    i = 2
    while i < n:
        if n % i == 0:
            return False
        i += 1
    return True

def sum_primes(n):
    """Calculates sum of all primes below given integer n"""
    return Reduce(
            lambda L: sum(args[0] for (args, p) in L if p),
            map(isprime, xrange(2, n)))

if __name__ == '__main__':
    print sum_primes(20)

Run the script, point your browser to http://localhost:8080/ and after some time the script will print the response.