测试Python多线程时遇AttributeError:builtin_function_or_method无find属性
问题描述
测试Python多线程功能时,日志输出大量冗余信息,同时触发错误:AttributeError: 'builtin_function_or_method' object has no attribute 'find'
原代码
import threading import time import logging import time def wait_func(time_to_wait,name): logging.info(f'{name} waiting..') time.sleep(time_to_wait) logging.info(f'{name} done waiting') logging.basicConfig(format=format, level=logging.INFO) threads = list() for i in range(3): name = 't' + str(i) logging.info(f'starting {name}') x = threading.Thread(target=wait_func, args=(i*3,name)) threads.append(x) x.start() for index, thread in enumerate(threads): logging.info(f"Main : before joining thread {index}.") thread.join() logging.info(f"Main : thread {index} done")
报错信息
--- Logging error --- Traceback (most recent call last): File "/Users/moninghoffjc/anaconda3/envs/psychopyNEW/lib/python3.6/logging/__init__.py", line 994, in emit msg = self.format(record) File "/Users/moninghoffjc/anaconda3/envs/psychopyNEW/lib/python3.6/logging/__init__.py", line 840, in format return fmt.format(record) File "/Users/moninghoffjc/anaconda3/envs/psychopyNEW/lib/python3.6/logging/__init__.py", line 578, in format if self.usesTime(): File "/Users/moninghoffjc/anaconda3/envs/psychopyNEW/lib/python3.6/logging/__init__.py", line 546, in usesTime return self._style.usesTime() File "/Users/moninghoffjc/anaconda3/envs/psychopyNEW/lib/python3.6/logging/__init__.py", line 388, in usesTime return self._fmt.find(self.asctime_search) >= 0 AttributeError: 'builtin_function_or_method' object has no attribute 'find' Call stack: File "/Users/moninghoffjc/anaconda3/envs/psychopyNEW/lib/python3.6/threading.py", line 884, in _bootstrap self._bootstrap_inner() File "/Users/moninghoffjc/anaconda3/envs/psychopyNEW/lib/python3.6/threading.py", line 916, in _bootstrap_inner self.run() File "/Users/moninghoffjc/anaconda3/envs/psychopyNEW/lib/python3.6/threading.py", line 864, in run self._target(*self._args, **self._kwargs) File "<ipython-input-18-ae212a132b6e>", line 7, in wait_func logging.critical(f'{name} waiting..') Message: 't0 waiting..' Arguments: ()
解决方法
1. 修复核心错误
报错根源是logging.basicConfig的format参数误用:你传入了Python内置函数format,而非日志格式字符串。日志模块尝试调用该函数的find方法,直接触发了属性错误。
替换成标准日志格式字符串即可,比如包含时间、线程名、日志级别和消息的格式:
logging.basicConfig(format='%(asctime)s - %(threadName)s - %(levelname)s - %(message)s', level=logging.INFO)
2. 优化日志冗余
- 删除重复的
import time语句 - 给线程显式指定名字,替代手动拼接的
t0/t1/t2,日志可读性更强 - 通过格式字符串区分主线程与子线程的日志输出
修复后的完整代码
import threading import time import logging def wait_func(time_to_wait, name): logging.info(f'{name} waiting..') time.sleep(time_to_wait) logging.info(f'{name} done waiting') # 配置清晰的日志格式,包含时间、线程名、级别、消息 logging.basicConfig( format='%(asctime)s - %(threadName)s - %(levelname)s - %(message)s', level=logging.INFO ) threads = list() for i in range(3): name = 't' + str(i) logging.info(f'starting {name}') # 显式指定线程名 x = threading.Thread(target=wait_func, args=(i*3, name), name=name) threads.append(x) x.start() for index, thread in enumerate(threads): logging.info(f"MainThread: before joining thread {index}.") thread.join() logging.info(f"MainThread: thread {index} done")
优化后效果
日志输出结构规整,无冗余信息,且能清晰区分各线程活动,示例输出如下:
2024-05-20 14:35:12,456 - MainThread - INFO - starting t0 2024-05-20 14:35:12,457 - t0 - INFO - t0 waiting.. 2024-05-20 14:35:12,457 - MainThread - INFO - starting t1 2024-05-20 14:35:12,458 - t1 - INFO - t1 waiting.. 2024-05-20 14:35:12,458 - MainThread - INFO - starting t2 2024-05-20 14:35:12,459 - t2 - INFO - t2 waiting.. 2024-05-20 14:35:12,459 - MainThread - INFO - MainThread: before joining thread 0. 2024-05-20 14:35:12,459 - t0 - INFO - t0 done waiting 2024-05-20 14:35:12,460 - MainThread - INFO - MainThread: thread 0 done ...
内容的提问来源于stack exchange,提问作者Mae Mon
相关产品推荐
相关产品推荐

