0

Requirement :

  1. webservice to support initialization of training function of machine learning model and return success, which takes around 4 hours to complete.

  2. To support predict function on previously trained models.

  3. Both of above function should run in parallel in non blocking way.

we were able to achieve this by creating a task queue using celery and pushing the training function to the queue but wanted to know if there are better methods.

I looked for async webservice modules and found aiohttp. I wrote below sample code but seems like if I trigeer run_job function then predict function gets blocked.

from aiohttp import web


async def training_job():
     for i in range(100100):
        print(i)
     return i

async def predict(request):
    ## some logic
    text = "Value after logic"
    return web.Response(text=text)

async def run_job(request):
    result = await training_job()
    return web.Response(text="Done")

app = web.Application()
app.add_routes([web.get('/', predict),
                web.get('/run_job', run_job)])

web.run_app(app)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sach
  • 904
  • 8
  • 20
  • `training_job()` is not a *cooperative routine*, in that it blocks. Don't run blocking code in a asynchio code base, unless you can push that into an executor. – Martijn Pieters Nov 22 '18 at 12:00
  • This looks similar to a [question](https://stackoverflow.com/questions/52336742/python-asynchronous-rest-api-with-responses-which-rely-on-cpu-intensive-calculat) I asked a few weeks ago. The answer posted might be helpful for you too. – sjw Nov 22 '18 at 12:02
  • I think it would be better if the training part runs in an entirely different process and that the webserver only accepts requests and pushes data to a queue/database. This makes it easier to scale the training processes or webserver processes if needed separately – Metareven Nov 22 '18 at 12:03
  • If the API for running a training job can't be made to be cooperative (yield to the asyncio loop when blocked on something), you'll need to unblock the event loop by using a separate thread or child process. Use [an executor pool](https://docs.python.org/3/library/asyncio-eventloop.html#executing-code-in-thread-or-process-pools) or have celery manage such jobs. – Martijn Pieters Nov 22 '18 at 12:09
  • Thanks @MartijnPieters understood your point, will go with celery implementation. – Sach Nov 22 '18 at 12:15

0 Answers0