English 中文(简体)
我如何确保甲型read在目标功能完成后死亡?
原标题:How do I ensure that a Python thread dies after its target function completes?

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)
最佳回答

你们是否相信你的布局,只是在指挥期间才这样做。 也就是说,它应当做到等同。

背 景

这一指挥线将打开一条遥远的炮弹,以控制步枪——直升机的指挥,然后关闭。

但是,我怀疑法布里图书馆可能做到:

东道国

host$ ls -lht /tmp . .

当然,这并不提供真实的情况,但有一些不同的选择,可以保持一种没有互动的连接。 在某些情况下,这样做是可取的(例如,如果你在同一个东道国拥有许多指挥权,那么这种技术将重复现有的会议,而不是每次开新会议。 查阅文件,以提出论据,使这种会议能够或能够分开进行。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...