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?
这方面的一个例子是,我正在与以下方面合作:
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<>>,而上述代码在限定的舱面一号内,则在Iknow之后仍可看到以下内容,即表面不应再存在:
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)