7

Hi I have a problem with the django commands the thing is I need to stop the command if some condition happen, normally in python script I do this using sys.exit() because I don't want the script still doing things I try this with Django and doesn't work there is another way to stop the command running ?

Health and good things.

2
  • 1
    Can't you simply return from handler? Doing sys.exit() is not safe. It can leave your app in corrupted state. It should be avoided at all costs. Generally the only case where you would use sys.exit is when you want to exit with specific status code. But even in that case you only sys.exit at the top level and inside functions you throw exceptions. Other than that I don't see any practical use for sys.exit. Commented Jan 14, 2016 at 19:13
  • And also note that exceptions are exactly the tool designed for what you are trying to do. Commented Jan 14, 2016 at 19:19

2 Answers 2

11

from the docs:

from django.core.management.base import BaseCommand, CommandError
from polls.models import Poll

class Command(BaseCommand):
    help = 'Closes the specified poll for voting'

    def add_arguments(self, parser):
        parser.add_argument('poll_id', nargs='+', type=int)

    def handle(self, *args, **options):
        for poll_id in options['poll_id']:
            try:
                poll = Poll.objects.get(pk=poll_id)
            except Poll.DoesNotExist:
                raise CommandError('Poll "%s" does not exist' % poll_id)

            poll.opened = False
            poll.save()

            self.stdout.write(self.style.SUCCESS('Successfully closed poll "%s"' % poll_id))

i.e. you should raise a CommandError

though sys.exit generally ought to work fine too (I mean, you said it didn't for you - if it was me I'd be curious to work out why not anyway)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, I'm going to try that approach but I just only use that not in dangerous process just in exact validation for example there is no file in folder just exit and stop the whole script but I'm going to try and remember sys.exit() according with the docs you can easily use wit try and except statements.
But what if I have a python script with some commands that should run one by one. Is there a convinient way to go to the next command if there is an error in the command before? Because CommandError just exits the script completely, it's how it should behave i guess.
0

I am surprised to see these solutions, for neither of them works. Sys.exit() just finishes off the current thread and raising a 'CommandError' only starts exception handling. Nor does raising a KeyboardInterrupt have any effect. Perhaps these solutions once worked with earlier versions, or with the server started in a different context.

The one solution I came across here is _thread.interrupt_main(). It stops the server after neatly finishing the current request.

1 Comment

Well, the original post is not about stopping the server (why would you do that in the first place, it's security vulnerability of epic proportion) but stopping command. And indeed CommandError does what it promises, command ends with status_code != 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.