Python 为什么日志记录和未来不一起工作?

Python 为什么日志记录和未来不一起工作?,python,python-3.x,logging,multiprocessing,Python,Python 3.x,Logging,Multiprocessing,我尝试了以下脚本,但日志不起作用 import time import concurrent.futures import logging import os ######## Logging ################ logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) file_handler = logging.FileHandler(os.path.join('logs','pipeline2.

我尝试了以下脚本,但日志不起作用

import time  
import concurrent.futures
import logging
import os

######## Logging ################
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
file_handler = logging.FileHandler(os.path.join('logs','pipeline2.log'))
#In case we have also a StreamHandler with level of INFO
file_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

def f(x):
    time.sleep(0.001)  # to visualize the progress
    logger.info(x**2)

def run(f, my_iter):
    """
    https://stackoverflow.com/questions/51601756/use-tqdm-with-concurrent-futures
    """
    import concurrent.futures
    my_iter = list(my_iter)
    l = len(my_iter)
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
        futures = {executor.submit(f, arg): arg for arg in my_iter}
        results = {}
        for future in concurrent.futures.as_completed(futures):
            arg = futures[future]
            results[arg] = future.result()

my_iter = range(10000)
run(f, my_iter)
运行脚本时:我没有看到日志中显示的任何文件pipeline2.log。我如何解决这个问题


编辑:正如建议的那样,我在代码中检索tqdm(我认为它不能干涉)n,这样它就可以很容易地复制。

如何运行代码?我可以想到以下情况:日志不工作

import time  
import concurrent.futures
import logging
import os

######## Logging ################
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
file_handler = logging.FileHandler(os.path.join('logs','pipeline2.log'))
#In case we have also a StreamHandler with level of INFO
file_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

def f(x):
    time.sleep(0.001)  # to visualize the progress
    logger.info(x**2)

def run(f, my_iter):
    """
    https://stackoverflow.com/questions/51601756/use-tqdm-with-concurrent-futures
    """
    import concurrent.futures
    my_iter = list(my_iter)
    l = len(my_iter)
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
        futures = {executor.submit(f, arg): arg for arg in my_iter}
        results = {}
        for future in concurrent.futures.as_completed(futures):
            arg = futures[future]
            results[arg] = future.result()

my_iter = range(10000)
run(f, my_iter)
导入时间
进口期货
导入日志记录
导入操作系统
logger=logging.getLogger(_名称__)
log\u path=os.path.join('logs','pipeline2.log'))
def添加文件处理程序(日志路径):
logger.setLevel(logging.INFO)
file\u handler=logging.FileHandler(日志路径)
文件\u handler.setLevel(logging.INFO)
格式化程序=日志记录。格式化程序('%(asctime)s:%(levelname)s:%(name)s:%(message)s')
文件\u handler.setFormatter(格式化程序)
logger.addHandler(文件处理程序)
打印(f“添加了处理程序fileno{file_handler.stream.fileno()}”)
def删除_文件(文件路径):
打印(f“{filepath}存在:{os.path.exists(filepath)}”)
尝试:
打印(f“{filepath}删除:{os.remove(filepath)}”)
除FileNotFoundError外:
通过
def f(x):
时间。睡眠(0.001)#可视化进度
logger.info(x**2)
def运行(f,我的iter):
"""
https://stackoverflow.com/questions/51601756/use-tqdm-with-concurrent-futures
"""
进口期货
my_iter=列表(my_iter)
l=len(我的书)
以concurrent.futures.ThreadPoolExecutor(max_workers=10)作为执行器:
futures={executor.submit(f,arg):我的iter中arg的arg}
结果={}
对于并发的未来。期货。完成时(期货):
arg=期货[未来]
结果[arg]=future.result()

删除文件(日志路径)
添加文件处理程序(日志路径)
删除\u文件(日志\u路径)
my_iter=范围(10)
运行(f,我的iter)
尝试:
打开(日志路径)作为f:
打印(f.read())
除FileNotFoundError外:
打印(f“没有这样的文件或目录{log_path}”)
印刷品

然后跑

添加文件处理程序(日志路径)
my_iter=范围(10)
运行(f,我的iter)
对于logger.handlers中的处理程序:
打印(handler.stream.fileno())
打印(handler.stream.write(f“fileno:{handler.stream.fileno()}\n”))
打印(handler.stream.flush())
尝试:
打开(日志路径)作为f:
打印(f.read())
除FileNotFoundError外:
打印(f“没有这样的文件或目录{log_path}”)
输出

handler fileno 61 added
58
11
None
61
11
None
2020-10-23 23:31:27,149:INFO:__main__:0
2020-10-23 23:31:27,149:INFO:__main__:1
2020-10-23 23:31:27,157:INFO:__main__:36
2020-10-23 23:31:27,155:INFO:__main__:16
2020-10-23 23:31:27,155:INFO:__main__:25
2020-10-23 23:31:27,156:INFO:__main__:9
2020-10-23 23:31:27,150:INFO:__main__:4
2020-10-23 23:31:27,159:INFO:__main__:64
2020-10-23 23:31:27,159:INFO:__main__:49
2020-10-23 2

你所说的“它不起作用”到底是什么意思?如果你的问题是关于未来和日志记录的,为什么你的例子包括tdqm?如果其他人没有安装您的代码,那么他们将无法测试您的代码。请看。@mkrieger:我编辑了这个问题来解释我的意思。此外,我想用TQM测试它,因为我处理了很多文件(我放了一个玩具,想看看进度)