def run_child(): root = pso.root() stat = root.stat promise = root.signal print('stat', stat) print('signal', root.signal) promise.wait(0) log_msg("Wait successfull") log_msg("Shared object field = {}".format(root.obj.field)) root.obj.inc_field(5) log_msg("Shared object field + 5 = {}".format(root.obj.field)) log_msg('Starting counter is {}'.format(root.counter)) cycle_count = 0 attempt_count = 0 for i in range(root.cycle_count): with transaction: attempt_count += 1 root.counter += 1 # Uncommend this and then try to find the error line number: # stat.sum += counter stat.sum += root.counter stat.count = root.counter cycle_count += 1 log_msg('Cycle count {}, attempted cycles {}'.format(cycle_count, attempt_count)) log_msg('Starting counter is {}'.format(root.counter2)) cycle_count2 = 0 attempt_count2 = 0 stat2 = root.stat2 root.signal2.wait(0) for i in range(root.cycle_count): attempt_count2 += 1 root.counter2 += 1 # Uncommend this and then try to find the error line number: # stat.sum += counter stat2.sum += root.counter2 stat2.count = root.counter2 cycle_count2 += 1 log_msg('Non-transacted cycle count {}, attempted cycles {}'.format(cycle_count2, attempt_count2))
class Account(pso.ShmObject): def __init__(this, id, starting_value): this.id = id this.balance = starting_value number_of_accounts = 200 print('sys.argv: ', sys.argv) if len(sys.argv) != 3 or sys.argv[1] != 'worker': # main process coord_name = pso.init() pso.root().worker_count = 3 if len(sys.argv) >= 2: pso.root().worker_count = int(sys.argv[1]) pso.root().accounts = { f'client{i}': Account(i, random.randrange(1000)) for i in range(number_of_accounts) } #original_values = [client.balance for client in pso.root().accounts.values()] original_values = [ pso.root().accounts[f'client{i}'].balance for i in range(number_of_accounts) ] original_sum = sum(original_values) print(f'Original sum: {original_sum}') print(f'{original_values}')
status = pso.connect(sys.argv[1]) print('2. Connected to coordinator {}'.format(status)) pso.set_random_flinch(True) import test1 pso.transient_start() # not needed anymore test1.run_child() else: import subprocess print('1. my pid {}'.format(os.getpid())) coord_name = pso.init() pso.set_random_flinch(True) pso.set_debug_reclaimer(True) pso.transient_start() pso.pso(2) p = pso.ShmPromise() root = pso.root() root.signal = p root.signal2 = pso.ShmPromise() root.counter = 0 root.counter2 = 0 root.cycle_count = 20 * 1000 stat_dict = pso.ShmObject() root.stat = stat_dict stat_dict.sum = 0 root.stat2 = pso.ShmObject() root.stat2.sum = 0 mychild = subprocess.Popen([sys.executable, sys.argv[0], coord_name]) with pso.Transient(): time.sleep(0.2) try:
def run_parent(): root = pso.root() root.obj = SomeSharedClass() log_msg("Shared object field = {}".format(root.obj.field)) root.obj.inc_field(14) log_msg("Shared object field + 14 = {}".format(root.obj.field)) stat = root.stat log_msg('Sending promise fulfillment to child') promise = root.signal promise.signal(True) log_msg('Starting counter is {}'.format(root.counter)) # pso.transaction_start() # root['counter'] = root['counter'] + 1 # pso.transaction_commit() cycle_count = 0 attempt_count = 0 for i in range(root.cycle_count): with transaction: attempt_count += 1 root.counter += 1 stat.sum += root.counter stat.count = root.counter cycle_count += 1 print('root.__dict__: ', len(root.__dict__)) log_msg('Cycle count {}, attempted cycles {}'.format(cycle_count, attempt_count)) log_msg('Sending second promise fulfillment to child') root.signal2.signal(True) log_msg('Non-transacted starting counter is {}'.format(root.counter2)) cycle_count2 = 0 attempt_count2 = 0 stat2 = root.stat2 for i in range(root.cycle_count): attempt_count2 += 1 root.counter2 += 1 stat2.sum += root.counter2 stat2.count = root.counter2 cycle_count2 += 1 log_msg('Non-transacted cycle count {}, attempted cycles {}'.format(cycle_count2, attempt_count2)) log_msg('new pso.ShmObject(): ' + str(pso.ShmObject() ) ) repr(root.counter) value = pso.ShmValue('teст') value2 = pso.ShmValue(b'test2') value3 = pso.ShmValue(True) value4 = pso.ShmValue(None) log_msg('value: ' + repr(value) + ';\n value2: ' + repr(value2) + ';\n value3: ' + repr(value3) + ';\n value4: ' + repr(value4) ) l = pso.ShmList() l.append(value3) log_msg('new pso.ShmList(): ' + repr(l) ) l.append(value4) log_msg('appended 1 to ShmList: ' + repr(l) ) l.append(value2) l.append(value) with transaction: itr = iter(l) log_msg('ShmListIter: ' + repr(itr) ) log_msg('First item: ' + repr(next(itr)) ) next(itr) next(itr) log_msg('Fourth item: ' + repr(next(itr)) ) log_msg('Iterating ShmList:') with transaction: for item in l: print(item) log_msg('Converting ShmList to list:') native_list = list(l) for item in native_list: print(item) list2 = pso.ShmList([1, 2, 3]) log_msg('Iterating another ShmList:') with transaction: for item in list2: print(item) pso.root().list3 = [1, 2, 3] list3 = pso.root().list3 list3[0] = 'First' list3_correct = ['First', 2, 3] for i in range(3): if list3_correct[i] != list3[i]: raise Exception('list3_correct[{}] != list3[{}]: {} != {}'.format(i, i, list3_correct[i], list3[i])) log_msg('Iterating implicitly created ShmList:') with transaction: for item in list3: print(item) d = pso.ShmDict() print('Just empty dict:', d) d['first'] = 'first value' d['second'] = 'second value' d['third'] = 'third value' d['fourth'] = 'fourth value' d['fifth'] = 'fifth value' log_msg('Iterating ShmDict:') with transaction: for item in d: print(item) log_msg('ShmDict keys: {0!r}'.format(d.keys())) log_msg('Converting ShmDict to dict:') native_dict = dict(d) with transaction: for item in native_dict: print(item) d2 = pso.ShmDict({'a': 2}) log_msg('ShmDict keys: {0!r}'.format(d2.keys())) log_msg(' values: {0!r}'.format(d2.values())) print('dict with value:', d['first']) del d['first'] try: print('dict empty again:', d['first']) except: traceback.print_stack() log_msg('Iterating tuple:') tpl = pso.ShmTuple((1, 2, 3,)) with transaction: for item in tpl: print(item) log_msg("Final shared object field = {}".format(root.obj.field)) root.obj.inc_field(3) log_msg("Final shared object field + 3 = {}".format(root.obj.field)) # test late iterator deletion # del itr del l log_msg("Test complete")
import sys import pso import subprocess if len(sys.argv) != 3: # main process coord_name = pso.init() pso.root().requests = [{ 'idx': i, 'request': f'request {i}' } for i in range(10)] pso.root().responses = [None] * 10 workers = [ subprocess.Popen( [sys.executable, sys.argv[0], coord_name, str(req['idx'])]) for req in pso.root().requests ] statuses = [w.wait(5) for w in workers] for (idx, response) in pso.root().responses: print((idx, response)) else: # worker process pso.connect(sys.argv[1]) myindex = int(sys.argv[2]) myrequest = pso.root().requests[myindex] import time, random time.sleep(random.randrange(2)) # imitate some long work pso.root().responses[myindex] = \ (myindex, f'response {myindex} for {myrequest["request"]}')
import subprocess class Account(pso.ShmObject): def __init__(this, id, starting_value): this.id = id this.balance = starting_value number_of_accounts = 200 if len(sys.argv) != 3 or sys.argv[1] != 'worker': # main process coord_name = pso.init() pso.root().worker_count = 3 if len(sys.argv) >= 2: pso.root().worker_count = int(sys.argv[1]) pso.root().accounts = { f'client{i}': Account(i, random.randrange(1000)) for i in range(number_of_accounts) } original_values = [ pso.root().accounts[f'client{i}'].balance for i in range(number_of_accounts) ] original_sum = sum(original_values) print(f'Original sum: {original_sum}') print(f'{original_values}')
return (None, True) else: return (rslt, False) def terminate(this): this.terminated = True this.promise.signal(True) number_of_accounts = 200 if len(sys.argv) != 3 or sys.argv[1] != 'worker': # main process coord_name = pso.init() pso.root().queue = queue = Queue() worker_count = 3 if len(sys.argv) >= 2: worker_count = int(sys.argv[1]) workers = [ subprocess.Popen([sys.executable, sys.argv[0], 'worker', coord_name]) for i in range(worker_count) ] items = [] ends = 0 while ends < worker_count: (item, eoq) = queue.pop() if eoq: ends += 1