【问题标题】:Python thread releasePython线程发布
【发布时间】: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() 函数后,线程t1t2 超出范围。但是,根据 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


【解决方案1】:
  • 进入 launch.json(Ctrl+Shift+P 并输入文件名)
  • 将启动选项的设置从 "type":"python" 更改为 "type":"pythonExperimental"

这似乎是 vscode 中的一个错误。该解决方案已发布以响应issue that you reported.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2011-02-24
    • 1970-01-01
    • 2011-05-11
    • 2019-06-05
    相关资源
    最近更新 更多