This commit is contained in:
PawelDopierala 2024-11-26 20:48:17 +01:00
parent 8b0322cd22
commit edcfb81f61
3 changed files with 62 additions and 10 deletions

View File

@ -3,7 +3,7 @@ from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType
from hcloud.locations.domain import Location
servers_no = 2
servers_no = 16
TOKEN = "V5gkzZ13coCVPKWkQbmbyGPyxDdsTjiubwVtx35jH7mix8A32JqM5CWJtqoLjtFK"
@ -57,4 +57,4 @@ with open('my_list.txt', 'w') as file:
for item in ip_list:
file.write(item + '\n')
print("Finished")
print("Finished")

38
execute.py Normal file
View File

@ -0,0 +1,38 @@
import asyncio
import httpx
with open('my_list.txt', 'r') as file:
ip_list = [line.strip() for line in file]
async def send_request(client, server_ip, rangee, sigma):
data = {
"a": rangee[0],
"b": rangee[1],
"sigma": sigma
}
response = await client.post(f"http://{server_ip}:8000/compute", json=data, timeout=300.0)
return response.text
# Główna funkcja do wysyłania zapytań do wszystkich serwerów równocześnie
async def main(a_start, b_end, sigma):
num_chunks = len(ip_list)
chunk_size = (b_end - a_start + 1) // num_chunks
ranges = [(a_start + i * chunk_size, min(a_start + (i + 1) * chunk_size - 1, b_end))
for i in range(num_chunks)]
async with httpx.AsyncClient() as client:
tasks = [send_request(client, server, rangee, sigma) for server, rangee in zip(ip_list, ranges)]
# tasks2 = [send_request(client, server, rangee, sigma) for server, rangee in zip(ip_list, ranges[16:32])]
# tasks.extend(tasks2)
results = await asyncio.gather(*tasks)
final_result = 0
for idx, result in enumerate(results):
final_result += int(result.strip('"'))
final_result = final_result % 999999937
print(final_result)
# Uruchomienie głównej funkcji
if __name__ == "__main__":
sigma = 510104288
a = 1
b = 7000
asyncio.run(main(a, b, sigma))

30
main.py
View File

@ -1,3 +1,5 @@
from concurrent.futures import ThreadPoolExecutor
from fastapi import FastAPI
from pydantic import BaseModel
import subprocess
@ -11,19 +13,31 @@ class ComputeInput(BaseModel):
b: str
sigma: str
@app.post("/compute")
async def compute(data: ComputeInput):
a = data.a
b = data.b
sigma = data.sigma
def run_compute(a,b,sigma):
result = subprocess.run(
['./computeC', a, b, sigma],
['./computeC', str(a), str(b), sigma],
capture_output=True,
text=True
)
return result.stdout.strip()
@app.post("/compute")
async def compute(data: ComputeInput):
a_start = int(data.a)
b_end = int(data.b)
sigma = data.sigma
num_chunks = 8
chunk_size = (b_end - a_start + 1) // num_chunks
ranges = [(a_start + i * chunk_size, min(a_start + (i + 1) * chunk_size - 1, b_end))
for i in range(num_chunks)]
with ThreadPoolExecutor() as executor:
futures = [executor.submit(run_compute, a, b, sigma) for a, b in ranges]
results = [f.result() for f in futures]
return sum(results)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)