88# @Package:
99# @Software: PyCharm
1010import ctypes
11+ import gc
12+ import os
13+ import platform
1114import sys
1215from subprocess import call
13- from pip . _internal . utils . misc import get_installed_distributions
14- import pkg_resources
16+
17+ import psutil
1518
1619
1720def check_version ():
@@ -33,6 +36,8 @@ def update_lib():
3336 更新依赖方式一
3437 :return:
3538 """
39+ from pip ._internal .utils .misc import get_installed_distributions
40+
3641 packages = [dist .project_name for dist in get_installed_distributions ()]
3742 call ("pip install --upgrade" + ' ' .join (packages ), shell = True )
3843
@@ -42,6 +47,8 @@ def update_lib_two():
4247 更新依赖方式二
4348 :return:
4449 """
50+ import pkg_resources
51+
4552 packages = [dist .project_name for dist in pkg_resources .working_set ]
4653 call ("pip install --upgrade" + ' ' .join (packages ), shell = True )
4754
@@ -127,5 +134,28 @@ def update_fire_wall(key_name='PublicProfile'):
127134 winreg .CloseKey (regRoot )
128135
129136
137+ def restart_process (path ):
138+ """
139+ 当内存占用达到一定比例进程重启
140+ :param path: 执行脚本的全路径
141+ :return:
142+ """
143+ if psutil .virtual_memory ().percent < 80 :
144+ print ('内存使用:' , psutil .Process (os .getpid ()).memory_info ().rss )
145+ print ("当前内存占用率:" , psutil .virtual_memory ().percent )
146+ # if gc.isenabled():
147+ # # 释放内存
148+ # gc.collect()
149+ print ("前进程id:" , os .getpid (), "父进程id:" , os .getppid ())
150+
151+ py = "python3" if (os .system ("python3 -V" ) == 0 ) else "python"
152+ sysstr = platform .system ()
153+ if sysstr == "Windows" :
154+ os .system (f"taskkill /pid { os .getpgid ()} /f && { py } { path } " )
155+ elif sysstr == "Linux" :
156+ os .system (f"kill -9 { os .getpgid ()} && { py } { path } " )
157+
158+
130159if __name__ == '__main__' :
131- print (get_windows_software ())
160+ # print(get_windows_software())
161+ restart_process (os .path .abspath (__file__ ))
0 commit comments