Navigation

    蓝鲸ROS机器人论坛

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Popular
    ROS交流群
    ROS Group
    产品服务
    Product Service
    开源代码库
    Github
    官网
    Official website
    技术交流
    Technological exchanges
    激光雷达
    LIDAR
    ROS教程
    ROS Tourials
    深度学习
    Deep Learning
    机器视觉
    Computer Vision

    django test 无法正常退出的问题

    技术交流
    1
    1
    2688
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • weijiz
      weijiz last edited by weijiz

      在用django的时候如果你在程序里开了其他线程(比如用作后台的长期服务程序)。那么在用django自带的测试程序就会有问题。测试完成之后程序无法自动退出。这是由于开启的线程还在运行的原因。

      解决方法也比较简单。这是因为django的测试代码中用的是sys.exit()来退出。但是当python程序还有其他线程在执行的时候这个指令是没办法退出的。
      把 django/core/management/commands/test.py

      def handle(self, *test_labels, **options):
              from django.conf import settings
              from django.test.utils import get_runner
      
              TestRunner = get_runner(settings, options['testrunner'])
      
              test_runner = TestRunner(**options)
              failures = test_runner.run_tests(test_labels)
      
              if failures:
                  sys.exit(1)
      

      改为

      def handle(self, *test_labels, **options):
              from django.conf import settings
              from django.test.utils import get_runner
      
              TestRunner = get_runner(settings, options['testrunner'])
      
              test_runner = TestRunner(**options)
              failures = test_runner.run_tests(test_labels)
      
              if failures:
                  os._exit(1)
              os._exit(0)
      

      就是采用os._exit()进行退出。这样其他线程能够收到SIGINT的信号,然后回收资源退出就可以了。

      1 Reply Last reply Reply Quote 0
      • First post
        Last post
      Copyright © 2015-2021 BlueWhale community