# iotest.py import time import threading from socket import * import itertools def task_pidigits(): """Pi calculation (Python)""" _map = map _count = itertools.count _islice = itertools.islice def calc_ndigits(n): # From http://shootout.alioth.debian.org/ def gen_x(): return _map(lambda k: (k, 4*k + 2, 0, 2*k + 1), _count(1)) def compose(a, b): aq, ar, as_, at = a bq, br, bs, bt = b return (aq * bq, aq * br + ar * bt, as_ * bq + at * bs, as_ * br + at * bt) def extract(z, j): q, r, s, t = z return (q*j + r) // (s*j + t) def pi_digits(): z = (1, 0, 0, 1) x = gen_x() while 1: y = extract(z, 3) while y != extract(z, 4): z = compose(z, next(x)) y = extract(z, 3) z = compose((10, -10*y, 0, 1), z) yield y return list(_islice(pi_digits(), n)) return calc_ndigits, (50, ) def spin(): task,args = task_pidigits() while True: r= task(*args) def echo_server(): s = socket(AF_INET, SOCK_DGRAM) s.setsockopt(SOL_SOCKET, SO_REUSEADDR,1) s.bind(("",16000)) while True: msg, addr = s.recvfrom(16384) s.sendto(msg,addr) # Launch threads NUMTHREADS = 2 for n in range(NUMTHREADS): t = threading.Thread(target=spin) t.setDaemon(True) t.daemon = True t.start() # Launch a background echo server echo_server()