【发布时间】:2018-03-21 15:04:24
【问题描述】:
我在python中运行2个线程并执行函数Do()。
import sys, threading
def do():
print ("Execute")
def run():
def start_thread():
thread = threading.Thread(target = do)
thread.start()
thread.join()
return thread
t1 = start_thread()
t2 = start_thread()
run()
print('Press enter to Quit')
sys.stdin.readline()
运行run() 函数后,线程t1 和t2 超出范围。但是,根据 VS-Code,它们仍处于 Running 模式。
我等他们到join()。这意味着它们被终止。那么,它们是如何运行的呢?如何安全释放这些线程?
【问题讨论】:
-
无法在 VSCode 之外复制这种情况,我猜这是编辑器的问题,而不是 Python 运行时的问题。您的代码实际上不能有 2 个并发运行线程,因为您
join第一个线程在starting 第二个线程之前。 -
您使用哪个 IDE 进行了测试?有人可以确认这是 VS-Code 中的错误吗?
-
我在终端运行它,htop 在第二个终端运行,python 上的过滤器打开(你可以用 F4 打开它)。线程一加入,它们就从 htop 中消失了。我在 Linux 上。
-
我刚刚报道了这个。网址 = github.com/Microsoft/vscode-python/issues/1191
标签: python python-3.x visual-studio-code python-multithreading