whyliyi 发表于 2014-08-29 17:24

python多线程无法写入日志

一个简单的测试,两个线程,在线程中打印日志,日志打印到文件中,发现多线程无法打印日志。
thread1.py代码如下:import logger.log
import threading
import t

log = logger.log.Logger.getLogger(__name__)

def thread1():
    t.test('1')

def thread2():
    t.test('2')


if __name__ == '__main__':
    log.debug('start main')
    t1 = threading.Timer(10.0, thread1)
    t1.start()
    t2 = threading.Timer(10.0, thread2)
    t2.start()

    t1.join()
    t2.join()t.py代码如下:import logging
import logging.config

logging.config.fileConfig('../conf/log.conf')
log = logging.getLogger(__name__)

def test(name):
    log.debug( 'thread' + name)日志配置文件如下:# logger configure file


keys=root,example


keys=consoleHandler,rotateFileHandler


keys=simpleFormatter


format=[%(asctime)s][%(levelname)s][%(name)s][%(filename)s:%(lineno)s]:%(message)s


level=DEBUG
handlers=consoleHandler,rotateFileHandler


level=DEBUG
handlers=consoleHandler,rotateFileHandler
qualname=example
propagate=0


class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)


class=handlers.RotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=('hcagent.log', 'a', 200000, 10)结果打印的日志如下::start main
:start main
:start main
:start main
:start main
问题:按理说在t.py里边打印的日志,应该能够打到hcagent.log里边?可是为什么没有呢?多线程需要做什么特殊处理?
恳请各位大神赐招

淡定与洒脱 发表于 2014-08-29 19:33

据说python多线程无法使用多core,与单线程性能一样?楼主正好测试下,看网上传言是否属实。

whitelotus19 发表于 2014-08-29 21:21

本帖最后由 whitelotus19 于 2014-08-30 09:58 编辑

..........

whyliyi 发表于 2014-08-30 08:46

没太看懂什么意思?能给点提示吗?
我t.py也有自己的日志设置的呀

回复 3# whitelotus19


   

whitelotus19 发表于 2014-08-30 10:19

我在3楼的回复没注意看你的代码,只是大概看了下你的描述,一下理解错了。

我没有用过日志配置文件的方式,我这样试了下好像可以的吧:

thread1.py内容:#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import logging.handlers
#import logger.log
import threading
import t

#log = logger.log.Logger.getLogger(__name__)
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
logfn='e:/temp/python/test/main.log'
hd = logging.handlers.RotatingFileHandler(logfn,maxBytes=10000000,backupCount=5)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s :: %(message)s')
hd.setFormatter(formatter)
log.addHandler(hd)


def thread1():
    t.test('1')

def thread2():
    t.test('2')


if __name__ == '__main__':
    log.debug('start main')
    t1 = threading.Timer(10.0, thread1)
    t1.start()
    t2 = threading.Timer(10.0, thread2)
    t2.start()

    t1.join()
    t2.join()
t.py内容:#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import logging.config

#logging.config.fileConfig('./log.conf')
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
logfn='e:/temp/python/test/thread.log'
hd = logging.handlers.RotatingFileHandler(logfn,maxBytes=10000000,backupCount=5)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s :: %(message)s')
hd.setFormatter(formatter)
log.addHandler(hd)

def test(name):
    log.debug( 'thread' + name)
运行thread1.py后,看到的日志文件内容如下:
main.log里的:
2014-08-30 10:12:20,157 - __main__ - DEBUG :: start main

thread.log里的:
2014-08-30 10:12:30,157 - t - DEBUG :: thread2
2014-08-30 10:12:30,157 - t - DEBUG :: thread1

不知道是不是这个意思?

whyliyi 发表于 2014-08-30 14:39

这样可以了,但是有一个问题,我把程序封装成一个Windows服务程序后,又不能打印了。不知道为什么。
回复 5# whitelotus19


   

whitelotus19 发表于 2014-08-30 21:14

我看了下以前写的程序,应该可以的阿,逻辑结构大体跟你的程序相同,只是我是用的我前面贴的代码的那种写日志的方式,没有用配置文件的方式。

whitelotus19 发表于 2014-08-31 15:25

本帖最后由 whitelotus19 于 2015-03-14 16:19 编辑

我写了个demo
http://lotus.pixub.com/?p=438
如果有不对的地方欢迎指正

whyliyi 发表于 2014-09-18 12:32

whitelotus19 发表于 2014-08-31 15:25 static/image/common/back.gif
我写了个demo
如果有不对的地方欢迎指正

非常感谢,这样就可以了。

还有个问题,请问你知道linux 的服务程序怎么写吗??我不太会

whyliyi 发表于 2014-09-26 20:31

whitelotus19 发表于 2014-08-31 15:25 static/image/common/back.gif
我写了个demo
如果有不对的地方欢迎指正


一下是从你demo里边摘下来的日志,有个问题,想问一下,为什么这些日志都是打印的两行呢???
2014-08-31 15:08:07,545 – thread – INFO :: thread1: 0 info
2014-08-31 15:08:07,545 – thread – INFO :: thread2: 0 info
2014-08-31 15:08:07,561 – thread – INFO :: thread2: 1 info
2014-08-31 15:08:07,561 – thread – INFO :: thread1: 1 info
2014-08-31 15:08:07,576 – thread – INFO :: thread2: 2 info
2014-08-31 15:08:07,576 – thread – INFO :: thread1: 2 info
2014-08-31 15:08:07,592 – thread – INFO :: thread1: 3 info
2014-08-31 15:08:07,592 – thread – INFO :: thread2: 3 info
页: [1] 2
查看完整版本: python多线程无法写入日志