Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am using django and inserting data into database and downloading images. When I call the function it works fine but it blocks the main thread. To execute the process in the background I'm using :
views.py:
class get_all_data(View):
def post(self, request, *args, **kwargs):
subprocess.Popen(
['python get_images.py --data=data'],
close_fds=True,
shell = True
But when I call:
python get_images.py(data="data")
it works fine but it runs on the main thread.
How can I fix it ? BTW I'm using python 2.7.
Note: Please don't suggest Celery . I'm looking for something which runs the task async or any other alternative.
I'm using the following code to insert into my db:
get_images.py:
from models import Test
def get_image_all():
#get data from server
insert_to_Test(data="data")
if __name__ == "__main__":
import argparse
from .models import Test
import django
django.setup()
parser = argparse.ArgumentParser()
parser.add_argument('--from_date')
parser.add_argument('--data')
parser.add_argument('--execute', type=bool, default=False)
args = parser.parse_args()
print "args", args
get_image_all(**vars(args))
When I call subprocess.Popen then it does not insert data into my db but If I execute it by call the function then it inserts the data to db. Why does that happen?
–
–
–
–
First, import and use Django models after django.setup()
Second, make sure the environment the spawn process runs in is correct, mainly make sure you use the right Python interpreter in the right virtualenv (if you use it), and make sure the process runs in the correct working directory
Sample code:
if __name__ == "__main__":
import argparse
import django
django.setup()
from .models import Test
# Parse args
# Use Django models
Next you need to make sure subprocess
runs in the correct working directory, and activates your Python virtualenv (if any), roughly something like below:
subprocess.Popen(
'/path/to/virtualenv/bin/python get_images.py --data=data',
cwd='/path/to/project/working_directory/',
shell=True
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.