如何确保 Python 线程在其目标函数完成后终止?
我有一个生成线程的服务。
线程通过提供目标函数来启动。
当函数结束时,线程似乎不会“死亡”。我知道这一点是因为该线程与 Paramiko 建立了一些 SSH 连接(通过 Fabric),如果我执行 lsof
,我会看到 SSH 连接在函数完成后仍然处于活动状态。
如何确保线程在其目标函数完成时终止?
这是我正在使用的示例:
from time import sleep
from threading import Thread
from fabric.api import run, settings
def thread_func(host):
with settings(host_string=host):
run('ls -lht /tmp')
def spawn_thread(host):
t = Thread(
target=thread_func,
args=(host,)
)
t.start()
spawn_thread('node1.example.com')
while True:
sleep(1)
如果我运行 sudo lsof |当上面的代码处于无限循环中时,在另一个终端中运行 grep ssh 我会看到以下内容,即使在我知道线程不应再存在之后:
python 6924 daharon 3u IPv4 170520 0t0 TCP 10.1.1.173:47368->node1.example.com:ssh (ESTABLISHED)
python 6924 daharon 5u IPv4 170524 0t0 TCP 10.1.1.173:47369->node1.example.com:ssh (ESTABLISHED)
python 6924 6930 daharon 3u IPv4 170520 0t0 TCP 10.1.1.173:47368->node1.example.com:ssh (ESTABLISHED)
python 6924 6930 daharon 5u IPv4 170524 0t0 TCP 10.1.1.173:47369->node1.example.com:ssh (ESTABLISHED)
python 6924 6932 daharon 3u IPv4 170520 0t0 TCP 10.1.1.173:47368->node1.example.com:ssh (ESTABLISHED)
python 6924 6932 daharon 5u IPv4 170524 0t0 TCP 10.1.1.173:47369->node1.example.com:ssh (ESTABLISHED)
I have a service that spawns threads.
The threads are started by providing a target function.
It would appear that the thread doesn't "die" when the function ends. I know this because the thread makes some SSH connections with Paramiko (via Fabric), and if I do an lsof
I see the SSH connections are still active after the function completes.
How can I make sure that a thread dies when its target function completes?
Here is an example of what I am working with:
from time import sleep
from threading import Thread
from fabric.api import run, settings
def thread_func(host):
with settings(host_string=host):
run('ls -lht /tmp')
def spawn_thread(host):
t = Thread(
target=thread_func,
args=(host,)
)
t.start()
spawn_thread('node1.example.com')
while True:
sleep(1)
And if I run sudo lsof | grep ssh
in another terminal while the above code is in its infinite loop I'll see the following, even after I know that the thread should not exist anymore:
python 6924 daharon 3u IPv4 170520 0t0 TCP 10.1.1.173:47368->node1.example.com:ssh (ESTABLISHED)
python 6924 daharon 5u IPv4 170524 0t0 TCP 10.1.1.173:47369->node1.example.com:ssh (ESTABLISHED)
python 6924 6930 daharon 3u IPv4 170520 0t0 TCP 10.1.1.173:47368->node1.example.com:ssh (ESTABLISHED)
python 6924 6930 daharon 5u IPv4 170524 0t0 TCP 10.1.1.173:47369->node1.example.com:ssh (ESTABLISHED)
python 6924 6932 daharon 3u IPv4 170520 0t0 TCP 10.1.1.173:47368->node1.example.com:ssh (ESTABLISHED)
python 6924 6932 daharon 5u IPv4 170524 0t0 TCP 10.1.1.173:47369->node1.example.com:ssh (ESTABLISHED)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定您的结构模块仅在 ls 命令期间执行 ssh 操作吗? 等效的操作。
也就是说,它应该执行与ssh 主机 ls -lht /tmp
此命令行将打开远程 shell 来运行 ls -lht 命令,然后关闭。
但我怀疑 Fabric 库可能会执行相当于:
ssh host
host$ ls -lht /tmp
。
。
当然,它不提供真正的 tty,但有不同的 ssh 选项允许在没有交互式 tty 的情况下保持连接打开。这在某些情况下是可取的(例如,如果您在同一主机上运行大量命令,此技术将重用现有的 ssh 会话,而不是每次都打开新会话。检查文档以获取启用或禁用此类会话缓存的参数。
Are you sure your fabric module is doing the ssh only for the duration of the ls command. That is, it should be doing the equivalent of
ssh host ls -lht /tmp
This commandline will open a remote shell to run the ls -lht command and then shutdown.
But I suspect the Fabric library might be doing the equivalent of:
ssh host
host$ ls -lht /tmp
.
.
Of course it's not providing a real tty but there are different ssh options that allow keeping a connection open without an interactive tty. This would be desirable in certain cases (e.g., if you run lots of commands on the same host, this technique will reuse the existing ssh session instead of opening new session every time. Check the documentation for arguments to enable or disable such session caching.