Kjako

client.py

Apr 13th, 2021
509
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from multiprocessing.managers import BaseManager
  2. import sys, math
  3.  
  4. tasks_number = 3
  5.  
  6. def read(fname):
  7.     f = open(fname, "r")
  8.     nr = int(f.readline())
  9.     nc = int(f.readline())
  10.  
  11.     A = [[0] * nc for x in range(nr)]
  12.     r = 0
  13.     c = 0
  14.     for i in range(0,nr*nc):
  15.         A[r][c] = float(f.readline())
  16.         c += 1
  17.         if c == nc:
  18.             c = 0
  19.             r += 1
  20.     return A
  21.  
  22. def task_creator(A, X, tasks_number, in_queue):
  23.     rows = len(A)
  24.     x = math.ceil(rows/tasks_number)
  25.     for i in range(tasks_number):
  26.         b = (i+1)*x
  27.         if(b > rows):
  28.             b = rows
  29.         in_queue.put([A[i*x: b], X, [i*x, b]])
  30.  
  31. class QueueManager(BaseManager):
  32.     pass
  33.  
  34. fnameA = sys.argv[1] if len(sys.argv) > 1 else "A.dat"
  35. fnameX = sys.argv[2] if len(sys.argv) > 2 else "X.dat"
  36. ip = sys.argv[3] if len(sys.argv) > 3 else 'localhost'
  37. port = int(sys.argv[4]) if len(sys.argv) > 4 else 50000
  38. tasks_number = int(sys.argv[5]) if len(sys.argv) > 5 else 4
  39. A = read(fnameA)
  40. X = read(fnameX)
  41. QueueManager.register('in_queue')
  42. QueueManager.register('out_queue')
  43. m = QueueManager(address=(ip, port), authkey='blah'.encode())
  44. m.connect()
  45. out_queue = m.out_queue()
  46. in_queue = m.in_queue()
  47. task_creator(A, X, tasks_number, in_queue)
  48. in_queue.join()
  49. result = [0] * len(A)
  50. while not out_queue.empty():
  51.     [worker_result, task_range] = out_queue.get()
  52.     result[task_range[0]:task_range[1]] = worker_result
  53. f = open("result", "w")
  54. f.write(str(result))
  55.  
  56.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×