I am having an issue at work with a python project I am working on (I normally use PHP/Java so am lacking a bit of knowledge).
Bascially I have a python program that I have built using Flask that connects an inventory management system to Shopify using the Shopify Python API.
When the user triggers a function via an AJAX request, I need to start a function/process that updates products in the client’s Shopify store via the Shopify API. This takes about 2 hours (~7000 products plus have to pull them first from the inventory management system).
The issue is that I need a way that I can trigger this function/process, and even if the client closes their browser the function/process will continue running.
If there is any way I could update the front end with the current progress of this background function/process as well that would be swell.
If anyone knows of any library or resources for accomplishing this it would be much appreciated.
I have had a google, but all the solutions I can find seem to be for CLI scripts not Web scripts.
Thanks heaps,
Corey π
2
Answers
Normally you’d create a task and return to the user an id he can use to pool the status of said task.
Then you’d process the task in another containerprocessthread.
Celery is a Python library that can help you set this up.
Another common solution is to use a publishersubscriber design and use a distributed queue such as Kafka, RabbitMQ or even Redis.
In fact, Celery can use RabbitMQ or Redis as its message broker.
You need to handle this task asynchronously because it’s a long-running job that would dramatically reduce the performance of an HTTP response (if you wait untill it finishes).
Also, you may notice that you need to run this task in a separate process of the current process that serves your HTTP request. Because, web servers (Gunicorn, uWSGI, etc…) will spawn the process they had created and liberate the system ressources when they need. You can easly be in the case that the async process launched via Ajax will be interrupted and killed by the web server because you closed the browser (request closed). So,
threading
andcoroutines
are not the best tools for this task.This is why there is some cool
Task queue
projects that solves your problem. We may note:Redis
andRabbitMQ
as message brokersAnd many more !
And with the rise of
micro services
you can combine the power ofTask queues
andcontainers
and you can build a seperate container(s) that handles your long running tasks (and updates your databse(s) as your current case). Also, if you can’t usemicro services
architecture yet, you can build a seperate server that handles those tasks and keep the web server that handles the user requests free from running a long running tasks.Finally, you can combine these solutions in your current website like this scenario:
Task ID
of the taskTask ID
by API or whatever and you add it in the session cookies or in seperate table that deals with the user who launched the process.Task ID
you have (in the user session cookies or in your database)And sure you can improve this scenario !