Created
June 15, 2014 13:43
-
-
Save flyer103/053d446cc2bf69de6e9e to your computer and use it in GitHub Desktop.
测试不同版本的 python 解释器退出时对 globals() 中 objects 的处理.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
"""测试不同版本的 python 解释器退出时对 globals() 中 objects 的处理. | |
python3.4 在解释器退出时不会将 globals() 中的 objects 置为 None,而 | |
之前的解释器会。而 globals() 中的变量顺序是随机的,因此会造成一些奇怪的现象. | |
""" | |
import os | |
import sys | |
import time | |
class Test(object): | |
def __init__(self, name): | |
self.name = name | |
# 设置文件 pid | |
self.fname_pid = self._write_pid() | |
def __del__(self): | |
# import os | |
print(globals()) | |
os.remove(self.fname_pid) | |
def _write_pid(self): | |
dir_home = os.getenv('HOME') | |
dir_pids = os.path.join(dir_home, 'pids') | |
if not os.path.isdir(dir_pids): | |
os.mkdir(dir_pids) | |
fname_pid = 'pid_{0}'.format(__file__.split('.')[0]) | |
fname_pid = os.path.join(dir_pids, fname_pid) | |
for postfix in range(100): | |
if os.path.isfile(fname_pid): | |
fname_pid = '{0}_{1}'.format(fname_pid, postfix) | |
continue | |
else: | |
break | |
with open(fname_pid, 'w') as fp_pid: | |
fp_pid.write(str(os.getpid())) | |
return fname_pid | |
def run(self): | |
while True: | |
print(self.name) | |
time.sleep(2) | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
sys.exit('Usage: $ python Test names') | |
name = sys.argv[1] | |
test = Test(name) | |
try: | |
test.run() | |
except KeyboardInterrupt: | |
print('Thank you for using.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment