Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python:running command in the background

Tags:

python

django

I have a fairly complex report generating work, which needs to be generated spaning through multiple tables. Final report is created in an excel sheet.The process takes anything between 10-20 minutes.

We have provided a Django web app for the client. Only issue if the client requests for a report, we are generating a URL and this takes time, and is an issue as far as the UI go.

I would like the task to be running behind, and when over, it can mail the client a link, with the report. What is the right strategy, and libraries to be used?

like image 773
ramdaz Avatar asked Apr 01 '26 03:04

ramdaz


2 Answers

You can use http://celeryproject.org/, it works like a charm, it has nice Django integration, it's very well documented and used by many.

like image 108
skrat Avatar answered Apr 02 '26 19:04

skrat


May be the below program helps you.

background.py

import subprocess

def background_execute(command):
    # launching the command
    p1 = subprocess.Popen(command,shell=True,stdout=True)
    p1.wait()
    print "Wait over, child exits"
    <Here you can add code to send mail>

Usage:

background_execute(["python sleep.py"])

sleep.py

import time

print "I am in sleep script, and will sleep for 10 sec"
time.sleep(10)
print "exit from child"
like image 31
RakeshSJoshi Avatar answered Apr 02 '26 18:04

RakeshSJoshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!