Python清理守护进程

时间:2017-10-22 06:25:16

标签: python daemon resource-cleanup

使用python 3.6。

关于清理守护程序(如进程或应用程序)的最佳做法是什么?

我了解情境管理人员,我不相信他们可以在这种情况下提供帮助。

考虑此代码:

from time import sleep
import asyncio

class RequiresResource(object):

    def __init__(self):
        self.file = open('test.txt', 'w')


    async def run_forever(self, msg, s=5):
        """Lets assume this function needs self.file"""
        while True:
            await asyncio.sleep(s)
            print(msg)


    def must_be_called(self):
        print('I was called')
        self.file.close()

resource = RequiresResource()
loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(asyncio.gather(resource.run_forever('Foo', 2), resource.run_forever('Bar',3)))
finally:
    resource.must_be_called()

resource.must_be_called()永远不会被称为

你会如何处理?

0 个答案:

没有答案
相关问题