from cloudbutton.multiprocessing import Pool, Lock, SimpleQueue, getpid import time def f(lock, q): with lock: pid = getpid() ts = time.time() msg = 'process: {} - timestamp: {}'.format(pid, ts) q.put(msg) time.sleep(1) if __name__ == "__main__": lock = Lock() q = SimpleQueue() n = 3 with Pool() as p: p.map_async(f, [[lock, q]] * n) for _ in range(n): print(q.get())
# from multiprocessing import Pool from cloudbutton.multiprocessing import Pool import random def is_inside(n): count = 0 for i in range(n): x = random.random() y = random.random() if x * x + y * y < 1: count += 1 return count if __name__ == '__main__': np, n = 96, 15000000000 part_count = [int(n / np)] * np pool = Pool(processes=np) count = pool.map(is_inside, part_count) pi = sum(count) / n * 4 print("Esitmated Pi: {}".format(pi))
#from multiprocessing import Pool from cloudbutton.multiprocessing import Pool def double(i): return i * 2 if __name__ == '__main__': with Pool() as pool: result = pool.map(double, [1, 2, 3, 4, 5]) print(result)
# from multiprocessing import Pool from cloudbutton.multiprocessing import Pool import random def is_inside(n): count = 0 for i in range(n): x = random.random() y = random.random() if x * x + y * y < 1: count += 1 return count if __name__ == '__main__': np, n = 96, 15000000000 part_count = [int(n / np)] * np pool = Pool(processes=np, initargs={'runtime_memory': 4096}) count = pool.map(is_inside, part_count) pi = sum(count) / n * 4 print("Esitmated Pi: {}".format(pi))