44 lines
1.0 KiB
Python
44 lines
1.0 KiB
Python
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
from fastapi import FastAPI
|
|
from pydantic import BaseModel
|
|
import subprocess
|
|
import uvicorn
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class ComputeInput(BaseModel):
|
|
a: str
|
|
b: str
|
|
sigma: str
|
|
|
|
def run_compute(a,b,sigma):
|
|
result = subprocess.run(
|
|
['./computeC', str(a), str(b), sigma],
|
|
capture_output=True,
|
|
text=True
|
|
)
|
|
return int(result.stdout.strip().strip('"'))
|
|
|
|
@app.post("/compute")
|
|
async def compute(data: ComputeInput):
|
|
a_start = int(data.a)
|
|
b_end = int(data.b)
|
|
sigma = data.sigma
|
|
|
|
num_chunks = 16
|
|
|
|
chunk_size = (b_end - a_start + num_chunks) // 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) % 999999937
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|