Python 3 threading.enumerate()异常

时间:2017-01-24 10:15:30

标签: django multithreading python-3.x exception enums

我正在编写基于django的web,我的应用程序正在运行。用户给出了一些参数,应用程序在自己的线程中启动。出于调试目的,我想打印以控制所有活动线程。我使用python线程模块和以下代码:

import threading

def some_func():
    print(threading.enumerate())
    .
    .
    .

并且此调用导致以下异常:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
    response = get_response(request)
  File "C:\Python34\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Python34\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\bakajsa\AppData\Local\Programs\Git\mysite\my_project\views.py", line 90, in some_func
    print(threading.enumerate())
  File "C:\Python34\lib\threading.py", line 824, in __repr__
    self.is_alive() # easy way to get ._is_stopped set when appropriate
  File "C:\Python34\lib\threading.py", line 1120, in is_alive
    self._wait_for_tstate_lock(False)
  File "C:\Python34\lib\threading.py", line 1076, in _wait_for_tstate_lock
    assert self._is_stopped
AssertionError

你知道什么可能出错吗?

根据文件:

threading.enumerate()

返回当前活动的所有Thread对象的列表。该列表包括守护线程,由current_thread()创建的虚拟线程对象和主线程。它排除了尚未启动的已终止线程和线程。

1 个答案:

答案 0 :(得分:0)

我认为这可能是打印问题

试试这个:

for thread in threading._enumerate(): print(thread.name)