【发布时间】:2014-07-29 18:25:55
【问题描述】:
我正在尝试编写一个小脚本,它将使用 plink.exe(来自同一文件夹)创建一个 ssh 隧道(在 Windows 上)。
我基本上是使用os.system 来启动命令:
import time
import threading
from os.path import join, dirname, realpath
pc_tunnel_command = '-ssh -batch -pw xxxx -N -L 1234:host1:5678 user@host2'
if __name__ == '__main__':
t = threading.Thread(target = os.system, \
args = (join(dirname(realpath(__file__)), 'plink.exe ') + \
pc_tunnel_command,))
t.daemon = True
t.start()
#without this line it will die. I guess that plink doesn't have enough time to start.
time.sleep(5)
print 'Should die now'
但是,线程(和 plink.exe)似乎继续运行。为什么会这样?有什么方法可以强制线程关闭?启动 plink 的更好方法?
我希望 plink.exe 在我的程序结束时死掉。使用守护线程是我让隧道在后台运行的计划,然后在我的主代码退出时死掉。
顺便说一句 - subprocess.call 也会发生同样的事情。
【问题讨论】:
标签: python multithreading python-2.7