小编use*_*971的帖子

多线程 - 覆盖队列中的旧值?

我有两个线程:生产者和消费者。生产者定期获取信息并将其提供给消费者。消费者只需要最新的信息副本,并且会不定期地检查它,可能会间隔很长时间。

促进这种交流的最简单机制似乎是创建一个Queue.Queue(maxsize=1). 但是,如果生产者在消费旧信息之前获取新信息,它将阻塞,直到消费者首先使用过时的信息。生产者有没有办法覆盖旧信息?

有没有更好的线程安全机制来实现这一点?

python queue multithreading

4
推荐指数
2
解决办法
2239
查看次数

Python - 从多个模块记录到旋​​转文件而不打印到控制台

我正在尝试将日志记录添加到一个中等规模的 Python 项目中,同时将中断降至最低。我想从多个模块记录到静默旋转文件(不将消息打印到终端)。我尝试修改这个示例,除了一个问题之外,我几乎拥有了我需要的功能。

以下是我尝试在主脚本中配置内容的方法:

import logging
import logging.handlers
import my_module

LOG_FILE = 'logs\\logging_example_new.out'

#logging.basicConfig(level=logging.DEBUG)

# Define a Handler which writes messages to rotating log files.
handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes=100000, backupCount=1)
handler.setLevel(logging.DEBUG)     # Set logging level.
# Create message formatter.
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
# Tell the handler to use this format
handler.setFormatter(formatter)

# Add the handler to the root logger
logging.getLogger('').addHandler(handler)

# Now, we can log to the root logger, or any other logger. First the …
Run Code Online (Sandbox Code Playgroud)

python logging

2
推荐指数
1
解决办法
2825
查看次数

标签 统计

python ×2

logging ×1

multithreading ×1

queue ×1