如何知道线程在python中是否是虚拟线程?

如何知道线程在python中是否是虚拟线程?,python,multithreading,apache2,apache2.4,Python,Multithreading,Apache2,Apache2.4,我的基本问题是:如何检测当前线程是否为伪线程?我是线程新手,最近我在Apache2/Flask应用程序中调试了一些代码,认为这可能有用。我遇到了一个触发器错误,请求在主线程上成功处理,在伪线程上失败,然后又在主线程上成功处理,等等 就像我说的,我使用的是Apache2和Flask,两者的结合创造了这些虚拟线程。如果有人能教我的话,我也很想知道更多 我的代码用于打印有关服务上运行的线程的信息,如下所示: def allthr_info(self): """Returns info in J

我的基本问题是:如何检测当前线程是否为伪线程?我是线程新手,最近我在Apache2/Flask应用程序中调试了一些代码,认为这可能有用。我遇到了一个触发器错误,请求在主线程上成功处理,在伪线程上失败,然后又在主线程上成功处理,等等

就像我说的,我使用的是Apache2和Flask,两者的结合创造了这些虚拟线程。如果有人能教我的话,我也很想知道更多

我的代码用于打印有关服务上运行的线程的信息,如下所示:

def allthr_info(self):
    """Returns info in JSON form of all threads."""
    all_thread_infos = Queue()
    for thread_x in threading.enumerate():
        if thread_x is threading.current_thread() or thread_x is threading.main_thread():
            continue
        info = self._thr_info(thread_x)
        all_thread_infos.put(info)

    return list(all_thread_infos.queue)

def _thr_info(self, thr):
    """Consolidation of the thread info that can be obtained from threading module."""
    thread_info = {}
    try:
        thread_info = {
            'name': thr.getName(),
            'ident': thr.ident,
            'daemon': thr.daemon,
            'is_alive': thr.is_alive(),
        }
    except Exception as e:
        LOGGER.error(e)
    return thread_info

您可以检查当前线程是否是
线程的实例。\u DummyThread

isinstance(threading.current_thread(), threading._DummyThread)
threading.py
本身可以教会您虚拟线程的含义:

虚拟线程类,用于表示此处未启动的线程。 他们死后不会被垃圾收集,也不能等待。 如果调用threading.py中调用current_thread()的任何内容,则 在活动目录中永远保留一个条目。 它们的目的是从当前线程()返回某些内容。 它们被标记为守护进程线程,因此我们不会等待它们 当我们退出时(符合前面的语义)

def current_thread():
    """Return the current Thread object, corresponding to the caller's thread of control.

    If the caller's thread of control was not created through the threading
    module, a dummy thread object with limited functionality is returned.

    """
    try:
        return _active[get_ident()]
    except KeyError:
        return _DummyThread()