在 Python 中调用 os.unlink(__file__) 安全吗?
我在 Linux 上使用 Python 2.6。
我有一个 run.py 脚本,它在后台启动多个服务并生成kill.py 来终止这些进程。
在kill.py内部,当它完成工作后取消自身链接是否安全?
import os
# kill services
os.unlink(__file__)
# is it safe to do something here?
我是Python新手。我担心的是,由于 Python 是一种脚本语言,整个脚本可能不在内存中。取消链接后,将不再有任何代码需要解释。
我尝试了这个小测试。
import os
import time
time.sleep(10) # sleep 1
os.unlink(__file__)
time.sleep(10) # sleep 2
当这个文件运行时,我运行了 stat Kill.py ,并且链接数始终为 1,所以我猜想 Python 解释器不保存该文件的链接。
作为一个更高级别的问题,创建稍后轻松终止的进程列表的常用方法是什么?
I'm using Python 2.6 on linux.
I have a run.py script which starts up multiple services in the background and generates kill.py to kill those processes.
Inside kill.py, is it safe to unlink itself when it's done its job?
import os
# kill services
os.unlink(__file__)
# is it safe to do something here?
I'm new to Python. My concern was that since Python is a scripting language, the whole script might not be in memory. After it's unlinked, there will be no further code to interpret.
I tried this small test.
import os
import time
time.sleep(10) # sleep 1
os.unlink(__file__)
time.sleep(10) # sleep 2
I ran stat kill.py
when this file was being run and the number of links was always 1, so I guess the Python interpreter doesn't hold a link to the file.
As a higher level question, what's the usual way of creating a list of processes to be killed later easily?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果可以避免的话,不要让你的脚本编写新脚本 - 只需写出 PID 列表,然后遍历它们。
目前还不清楚您要做什么,但创建和删除脚本听起来太脆弱了。
回答这个问题:
Python 会编译所有源代码并在执行之前关闭文件,因此这是安全的。
一般来说,在 Linux 上取消链接打开的文件是安全的。 (但并非在所有地方:在 Windows 上,您无法删除正在使用的文件。)
请注意,当您
导入
模块时,Python 2 会将其编译为.pyc
字节码文件并解释它。如果删除.py
文件,Python 仍将使用.pyc
,反之亦然。Don't have your scripts write new scripts if you can avoid it – just write out a list of the PIDs, and then through them.
It's not very clear what you're trying to do, but creating and deleting scripts sounds like too much fragile magic.
To answer the question:
Python compiles all of the source and closes the file before executing it, so this is safe.
In general, unlinking an opened file is safe on Linux. (But not everywhere: on Windows you can't delete a file that is in use.)
Note that when you
import
a module, Python 2 compiles it into a.pyc
bytecode file and interprets that. If you remove the.py
file, Python will still use the.pyc
, and vice versa.只是不要调用reload!
Python 不需要锁定文件,因为它们是在导入时编译和加载的。事实上,在程序运行时交换文件的能力通常非常有用。
Just don't call reload!
There's no need for Python to hold locks on the files since they are compiled and loaded at import time. Indeed, the ability to swap files out while a program is running is often very useful.
IIRC(!):当在 *nix 上时,取消链接仅删除文件系统中的名称,当最后一个文件句柄关闭时,inode 也会被删除。因此,这不会引起任何问题,除非 python 尝试重新打开该文件。
IIRC(!): When on *nix an unlink only removes the name in the filesystem, the inode is removed when the last file handle is closed. Therefore this should not induce any problems, except python tries to reopen the file.
我会将 PID 放入
list
中,并使用os.kill
对其进行迭代。我不明白您为什么要为此创建并执行新脚本。I would put the PIDs in a
list
and iterate over that withos.kill
. I don't see why you're creating and executing a new script for this.Python 会读取整个源文件并在执行之前对其进行编译,因此您不必担心删除或更改正在运行的脚本文件。
Python reads in a whole source file and compiles it before executing it, so you don't have to worry about deleting or changing your running script file.