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?

python get_images.py(data="data") What kind of shell are you using? This is definitely a syntax error under bash. – freakish Jun 6, 2017 at 9:00 @freakish I'm calling the method in this way and not using the shell : python get_images.py(data="data"). )Only the code with Popen is within a shell. – jason Jun 6, 2017 at 10:27 Show the content of get_images.py I suspect you're using things in it that would only run if executed inside a Django context (e.g. importing models) – bakkal Jun 6, 2017 at 12:26 @bakkal I'm sorry I had added the code under the wrong file. I've updated all changes. Please take a look :) – jason Jun 6, 2017 at 12:31
  • 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
                    I get this error now :  raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
    – jason
                    Jun 6, 2017 at 14:02
            

    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.