Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Python 3 function even after user has closed web browser/tab?

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 :)

like image 791
Fishingfon Avatar asked May 14 '20 07:05

Fishingfon


1 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 container\process\thread.

Celery is a Python library that can help you set this up.

Another common solution is to use a publisher\subscriber 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.

like image 125
Gabriel Cappelli Avatar answered Oct 01 '22 17:10

Gabriel Cappelli