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.

Alain Abrahan
  • 83
  • 2
  • 9
  • 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`. – freakish Jan 14 '16 at 19:13
  • And also note that exceptions are exactly the tool designed for what you are trying to do. – freakish Jan 14 '16 at 19:19

2 Answers2

8

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)

Anentropic
  • 32,188
  • 12
  • 99
  • 147
  • 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. – Alain Abrahan Jan 14 '16 at 21:37
1

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.

Community
  • 1
  • 1
user508402
  • 496
  • 1
  • 4
  • 19
  • 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 – Drachenfels Jul 21 '20 at 13:46