From e8adbeb4014d18faa7ef4b8a1ed61d1b04006616 Mon Sep 17 00:00:00 2001 From: marcsello Date: Thu, 24 Sep 2020 16:14:42 +0200 Subject: [PATCH] Implemented basic stuff --- kebab_worker/main.py | 49 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/kebab_worker/main.py b/kebab_worker/main.py index 84f4eac..bb764c9 100644 --- a/kebab_worker/main.py +++ b/kebab_worker/main.py @@ -1,9 +1,54 @@ +import multiprocessing import os +import requests +def worker(outqueue, id): + print(f"Process {id} is preparing...") + import numpy as np + import networkx as nx + + with open("/dkebab/GBA1000.txt") as f: + n = int(f.readline()) # pontok száma + m = int(f.readline()) # élek száma + A = [[0] * n for _ in range(n)] # táblázat, ahol az élek helyén 1 van, ammeg 0 + for _ in range(m): + [u, v] = map(int, f.readline().split()) + A[u][v] = 1 + A[v][u] = 1 + + input_graph = nx.Graph(np.array(A)) + + def max_comp_size(G): + return max([len(c) for c in nx.connected_components(G)]) + + print(f"Process {id} is working...") + while True: + del_list = [] + G = input_graph.copy() + target = len(G.nodes) / 2 + while max_comp_size(G) > target: + u = np.random.choice(G.nodes) + if u not in del_list: + del_list.append(u) + G.remove_node(u) + + outqueue.put(del_list) + def main(): - for i in range(os.cpu_count()): - print(i) + target_url = os.environ['TARGET_URL'] + multiprocessing.set_start_method('spawn') + outqueue = multiprocessing.Queue() + processes = [] + + for id in range(os.cpu_count()): + p = multiprocessing.Process(target=worker, args=(outqueue, id)) + p.start() + processes.append(p) + + while True: + result = outqueue.get() + requests.post(target_url, json=result) if __name__ == '__main__':