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) 
5
  • 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. Commented Nov 22, 2018 at 12:00
  • This looks similar to a question I asked a few weeks ago. The answer posted might be helpful for you too. Commented Nov 22, 2018 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 Commented Nov 22, 2018 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 or have celery manage such jobs. Commented Nov 22, 2018 at 12:09
  • Thanks @MartijnPieters understood your point, will go with celery implementation. Commented Nov 22, 2018 at 12:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.