Celery task for ML prediction hangs in execution
Thanks to this SO question I found the answer for my problem:
It turns out that is better for Keras to work with Threads
pooling instead of default Process
.
Lucky for me, with Celery 4.4 Threads
pooling was reintroduced not a long time ago.You can read more at Celery 4.4 Changelogs:
Threaded Tasks Pool
We reintroduced a threaded task pool using concurrent.futures.ThreadPoolExecutor.
The previous threaded task pool was experimental. In addition it was based on the threadpool package which is obsolete.
You can use the new threaded task pool by setting worker_pool to ‘threads` or by passing –pool threads to the celery worker command.
Now you can use threads instead of processes for pooling.
celery worker -A your_application --pool threads --loginfo=INFO
If you can not use latest Celery version, you have possibility to use gevent
package:
pip install geventcelery worker -A your_application --pool gevent --loginfo=INFO