cloud_3_3/main.py

44 lines
1.0 KiB
Python
Raw Permalink Normal View History

2024-11-26 20:48:17 +01:00
from concurrent.futures import ThreadPoolExecutor
2024-11-26 16:53:02 +01:00
from fastapi import FastAPI
from pydantic import BaseModel
import subprocess
import uvicorn
app = FastAPI()
class ComputeInput(BaseModel):
a: str
b: str
sigma: str
2024-11-26 20:48:17 +01:00
def run_compute(a,b,sigma):
2024-11-26 16:53:02 +01:00
result = subprocess.run(
2024-11-26 21:44:09 +01:00
['./computeC', str(a), str(b), sigma],
2024-11-26 16:53:02 +01:00
capture_output=True,
text=True
)
2024-11-26 21:07:28 +01:00
return int(result.stdout.strip().strip('"'))
2024-11-26 16:53:02 +01:00
2024-11-26 20:48:17 +01:00
@app.post("/compute")
async def compute(data: ComputeInput):
a_start = int(data.a)
b_end = int(data.b)
2024-11-26 21:44:09 +01:00
sigma = data.sigma
2024-11-26 20:48:17 +01:00
2024-11-26 21:20:45 +01:00
num_chunks = 16
2024-11-26 20:48:17 +01:00
2024-11-26 21:55:52 +01:00
chunk_size = (b_end - a_start + num_chunks) // num_chunks
2024-11-26 20:48:17 +01:00
ranges = [(a_start + i * chunk_size, min(a_start + (i + 1) * chunk_size - 1, b_end))
for i in range(num_chunks)]
2024-11-26 22:13:10 +01:00
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) % 999999937
2024-11-26 20:48:17 +01:00
2024-11-26 16:53:02 +01:00
if __name__ == "__main__":
2024-11-26 17:23:13 +01:00
uvicorn.run(app, host="0.0.0.0", port=8000)