导入静默杀死线程 [英] import silently kills thread

查看:66
本文介绍了导入静默杀死线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序Base.py,用于测试当模块不存在时import是否能够引发异常.

I have a simple program Base.py that tests if import is able to throw an exception when the module does not exist.

# Base.py
import threading, os, time
import_is_working = False

class Launch(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.start()
    def run(self):
        global import_is_working
        try:
            print "Yes, it got into the 'try' block"
            import NON_EXISTENT_MODULE
            assert False
        except:
            print "Great, your python language is working"
            import_is_working = True
Launch()
for i in range(500):
    time.sleep(0.01)
    if import_is_working:
        break
if not import_is_working:
    print "Your import is not working."
    os._exit(4)

有时候我喜欢在另一个模块Main.py中使用此代码:

Sometimes I like to use this code in another module Main.py:

# Main.py
import Base

令人惊讶的是,当我以这种方式运行它时,它不起作用:

Surprisingly, it doesn't work when I run it that way:

max% python Base.py
Yes, it got into the 'try' block
Great, your python language is working
max% python Main.py
Yes, it got into the 'try' block
Your import is not working.
max%

这在Ubuntu中发生,并且也在干净的CentOS 7.3中安装.

This happens in Ubuntu, and in a clean CentOS 7.3 install too.

推荐答案

您正在遇到导入锁定".

You're running into the "import lock".

文档提到了局限性在线程期间导入时,您违反了第一个(强调我的意思):

The documentation mentions the limitations in importing during threads, you're violating the first one (emphasis mine):

虽然进口机器是线程安全的,但有两个关键 由于内在的限制,对螺纹进口的限制 提供线程安全的方式:

While the import machinery is thread-safe, there are two key restrictions on threaded imports due to inherent limitations in the way that thread-safety is provided:

首先,除了在主模块中以外,导入不应产生派生新线程然后等待的副作用. .不遵守此限制可能会导致 如果产生的线程直接或间接尝试执行死锁,则死锁 导入模块.

Firstly, other than in the main module, an import should not have the side effect of spawning a new thread and then waiting for that thread in any way. Failing to abide by this restriction can lead to a deadlock if the spawned thread directly or indirectly attempts to import a module.

第二,必须先完成所有导入尝试,然后解释器才开始关闭自身.这可能是最容易的 通过仅从创建的非守护程序线程执行导入来实现 通过线程模块.守护程序线程和创建的线程 直接与线程模块配合使用将需要其他形式 同步以确保它们在系统运行后不尝试导入 关闭已开始.不遵守此限制将导致 在解释器关闭期间出现间歇性异常和崩溃(如 较晚的进口尝试访问不再存在于机器中的机器 有效状态).

Secondly, all import attempts must be completed before the interpreter starts shutting itself down. This can be most easily achieved by only performing imports from non-daemon threads created through the threading module. Daemon threads and threads created directly with the thread module will require some other form of synchronization to ensure they do not attempt imports after system shutdown has commenced. Failure to abide by this restriction will lead to intermittent exceptions and crashes during interpreter shutdown (as the late imports attempt to access machinery which is no longer in a valid state).

这篇关于导入静默杀死线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆