类::uu del uuu:尝试使用析构函数中的成员对象释放资源失败

2024-04-26 06:13:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在下面有一个简单的类Test,它启动一个Google计算引擎VM,然后通过调用函数(显式)或依赖对象销毁(通过__del__隐式)来停止它。你知道吗

class Test:
    def __init__(self):
        os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'secret.json'
        self._compute = googleapiclient.discovery.build('compute',
                                                        'v1',
                                                        cache_discovery=False)
        print('starting vm')
        req = self._compute.instances().start(project='test-project',
                                              zone='us-east1-b',
                                              instance='test-vm',
                                              requestId=uuid.uuid1())
        self.execute(req)
        self._running = True

    def stop(self):
        print('stopping vm')
        req = self._compute.instances().stop(project='test-project',
                                             zone='us-east1-b',
                                             instance='test-vm',
                                             requestId=uuid.uuid1())
        self.execute(req)
        self._running = False

    def execute(self, req):
        print(f'method({req.methodId})')
        resp = req.execute()
        while resp['status'] != 'DONE':
            time.sleep(1)
            resp = req.execute()
        return resp

    def __del__(self):
        if self._running:
            print('stopping in destructor')
            self.stop()

Test类的交互方式如下:

if __name__ == '__main__':
    print('start test')
    t = Test()

    print('vm running, wait 5 seconds')
    time.sleep(5)

    if len(sys.argv) == 2 and sys.argv[1] == 'stop':
        t.stop()

    print('test done')

当使用显式方法时(即:在对象开始被破坏之前调用t.stop()),VM将正常停止:

$ ./test.py stop
start test
starting vm
method(compute.instances.start)
vm running, wait 5 seconds
stopping vm
method(compute.instances.stop)
test done

但是,当使用隐式方法时(即:依赖于t超出范围,并调用了Test::__del__),我从googleapiclient堆栈中抛出了一个异常:

$ ./test.py 
start test
starting vm
method(compute.instances.start)
vm running, wait 5 seconds
test done
stopping in destructor
stopping vm
method(compute.instances.stop)
Exception ignored in: <bound method Test.__del__ of <__main__.Test object at 0x7fe091720320>>
Traceback (most recent call last):
  File "./test.py", line 46, in __del__
  File "./test.py", line 32, in stop
  File "./test.py", line 37, in execute
  File "/src/test/.venv/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
  File "/src/test/.venv/lib/python3.6/site-packages/googleapiclient/http.py", line 846, in execute
  File "/src/test/.venv/lib/python3.6/site-packages/googleapiclient/http.py", line 183, in _retry_request
  File "/src/test/.venv/lib/python3.6/site-packages/googleapiclient/http.py", line 164, in _retry_request
  File "/src/test/.venv/lib/python3.6/site-packages/google_auth_httplib2.py", line 198, in request
  File "/src/test/.venv/lib/python3.6/site-packages/httplib2/__init__.py", line 1926, in request
  File "/src/test/.venv/lib/python3.6/site-packages/httplib2/__init__.py", line 1595, in _request
  File "/src/test/.venv/lib/python3.6/site-packages/httplib2/__init__.py", line 1502, in _conn_request
  File "/usr/lib/python3.6/http/client.py", line 1239, in request
  File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
  File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
  File "/usr/lib/python3.6/http/client.py", line 1026, in _send_output
  File "/usr/lib/python3.6/http/client.py", line 986, in send
  File "/usr/lib/python3.6/ssl.py", line 975, in sendall
  File "/usr/lib/python3.6/ssl.py", line 944, in send
  File "/usr/lib/python3.6/ssl.py", line 642, in write
ssl.SSLError: Underlying socket connection gone (_ssl.c:2067)

问题:

  • 我使用Test::__del__停止虚拟机是否非法?你知道吗
  • 调用Test::__del__时,访问self._compute是否不再安全?你知道吗
  • 这是googleapi客户端中的一个bug吗?你知道吗
  • 有没有任何方法可以依靠销毁我的对象来触发停止我的虚拟机?你知道吗

Tags: inpytestselfsrcvenvlibline

热门问题