scan.py
Example on using array computations
Size 1.9 kB - File type text/python-sourceFile contents
#!/usr/bin/env python import os, sys, math, time import cPickle as pickle from random import uniform # Get task ID task_id = int(os.environ.get('SGE_TASK_ID', '0')) # Global parametets cache_filename = 'scan_cache.pickle' cache_filename_lock = 'scan_cache.pickle.lock' # Helper functions. def lock(): # Wait until cache is released, then lock it! while True: if os.path.isfile(cache_filename_lock): time.sleep(1) continue try: f = open (cache_filename_lock,'w') f.write(str(task_id)) f.flush() f.close() break except IOError, msg: print >> sys.stderr, str (msg) return def release(): # Release cache file lock. if os.path.isfile(cache_filename_lock): try: os.remove(cache_filename_lock) except OSError: pass return def get_cache(): # Read cache: cache = {} if os.path.isfile(cache_filename): f = open(cache_filename, 'r') cache = pickle.load(f) f.close() return cache def put_cache(cache): f = open(cache_filename,'w') pickle.dump(cache, f) f.flush() f.close() return # Process cache: if task_id==0: from pylab import plot, show x_list = [] y_list = [] lock() cache = get_cache() release() for x,y in cache.values(): x_list.append(x) y_list.append(y) plot(x_list, y_list, 'o') show() else: # Check if the current task has result already lock() cache = get_cache() release() if cache.has_key(task_id): # skip calculating known results pass else: # calculate x = task_id*math.pi/100.0 y = math.sin(x) time.sleep(uniform(5,25)) # Save results lock() cache = get_cache() cache[task_id] = (x,y) put_cache(cache) release() print 'Done' #EOF