亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: python多線程無法寫入日志 [打印本頁(yè)]

作者: whyliyi    時(shí)間: 2014-08-29 17:24
標(biāo)題: python多線程無法寫入日志
一個(gè)簡(jiǎn)單的測(cè)試,兩個(gè)線程,在線程中打印日志,日志打印到文件中,發(fā)現(xiàn)多線程無法打印日志。
thread1.py代碼如下:
  1. import logger.log
  2. import threading
  3. import t

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

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

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


  9. if __name__ == '__main__':
  10.     log.debug('start main')
  11.     t1 = threading.Timer(10.0, thread1)
  12.     t1.start()
  13.     t2 = threading.Timer(10.0, thread2)
  14.     t2.start()

  15.     t1.join()
  16.     t2.join()
復(fù)制代碼
t.py代碼如下:
  1. import logging
  2. import logging.config

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

  5. def test(name):
  6.     log.debug( 'thread' + name)
復(fù)制代碼
日志配置文件如下:
  1. # logger configure file

  2. [loggers]
  3. keys=root,example

  4. [handlers]
  5. keys=consoleHandler,rotateFileHandler

  6. [formatters]
  7. keys=simpleFormatter

  8. [formatter_simpleFormatter]
  9. format=[%(asctime)s][%(levelname)s][%(name)s][%(filename)s:%(lineno)s]:%(message)s

  10. [logger_root]
  11. level=DEBUG
  12. handlers=consoleHandler,rotateFileHandler

  13. [logger_example]
  14. level=DEBUG
  15. handlers=consoleHandler,rotateFileHandler
  16. qualname=example
  17. propagate=0

  18. [handler_consoleHandler]
  19. class=StreamHandler
  20. level=DEBUG
  21. formatter=simpleFormatter
  22. args=(sys.stdout,)

  23. [handler_rotateFileHandler]
  24. class=handlers.RotatingFileHandler
  25. level=DEBUG
  26. formatter=simpleFormatter
  27. args=('hcagent.log', 'a', 200000, 10)
復(fù)制代碼
結(jié)果打印的日志如下:
  1. [2014-08-29 16:48:16,132][DEBUG][__main__][thread1.py:17]:start main
  2. [2014-08-29 17:14:59,849][DEBUG][__main__][thread1.py:17]:start main
  3. [2014-08-29 17:15:59,180][DEBUG][__main__][thread1.py:17]:start main
  4. [2014-08-29 17:16:40,759][DEBUG][__main__][thread1.py:17]:start main
  5. [2014-08-29 17:20:26,417][DEBUG][__main__][thread1.py:17]:start main
復(fù)制代碼
問題:按理說在t.py里邊打印的日志,應(yīng)該能夠打到hcagent.log里邊?可是為什么沒有呢?多線程需要做什么特殊處理?
懇請(qǐng)各位大神賜招
作者: 淡定與灑脫    時(shí)間: 2014-08-29 19:33
據(jù)說python多線程無法使用多core,與單線程性能一樣?樓主正好測(cè)試下,看網(wǎng)上傳言是否屬實(shí)。
作者: whitelotus19    時(shí)間: 2014-08-29 21:21
本帖最后由 whitelotus19 于 2014-08-30 09:58 編輯

..........
作者: whyliyi    時(shí)間: 2014-08-30 08:46
沒太看懂什么意思?能給點(diǎn)提示嗎?
我t.py也有自己的日志設(shè)置的呀

回復(fù) 3# whitelotus19


   
作者: whitelotus19    時(shí)間: 2014-08-30 10:19
我在3樓的回復(fù)沒注意看你的代碼,只是大概看了下你的描述,一下理解錯(cuò)了。

我沒有用過日志配置文件的方式,我這樣試了下好像可以的吧:

thread1.py內(nèi)容:
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-

  3. import logging
  4. import logging.handlers
  5. #import logger.log
  6. import threading
  7. import t

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


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

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


  20. if __name__ == '__main__':
  21.     log.debug('start main')
  22.     t1 = threading.Timer(10.0, thread1)
  23.     t1.start()
  24.     t2 = threading.Timer(10.0, thread2)
  25.     t2.start()

  26.     t1.join()
  27.     t2.join()
復(fù)制代碼
t.py內(nèi)容:
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-

  3. import logging
  4. import logging.config

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

  13. def test(name):
  14.     log.debug( 'thread' + name)
復(fù)制代碼
運(yùn)行thread1.py后,看到的日志文件內(nèi)容如下:
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

不知道是不是這個(gè)意思?

作者: whyliyi    時(shí)間: 2014-08-30 14:39
這樣可以了,但是有一個(gè)問題,我把程序封裝成一個(gè)Windows服務(wù)程序后,又不能打印了。不知道為什么。
回復(fù) 5# whitelotus19


   
作者: whitelotus19    時(shí)間: 2014-08-30 21:14
我看了下以前寫的程序,應(yīng)該可以的阿,邏輯結(jié)構(gòu)大體跟你的程序相同,只是我是用的我前面貼的代碼的那種寫日志的方式,沒有用配置文件的方式。
作者: whitelotus19    時(shí)間: 2014-08-31 15:25
本帖最后由 whitelotus19 于 2015-03-14 16:19 編輯

我寫了個(gè)demo
http://lotus.pixub.com/?p=438
如果有不對(duì)的地方歡迎指正
作者: whyliyi    時(shí)間: 2014-09-18 12:32
whitelotus19 發(fā)表于 2014-08-31 15:25
我寫了個(gè)demo
如果有不對(duì)的地方歡迎指正


非常感謝,這樣就可以了。

還有個(gè)問題,請(qǐng)問你知道linux 的服務(wù)程序怎么寫嗎??我不太會(huì)
作者: whyliyi    時(shí)間: 2014-09-26 20:31
whitelotus19 發(fā)表于 2014-08-31 15:25
我寫了個(gè)demo
如果有不對(duì)的地方歡迎指正



一下是從你demo里邊摘下來的日志,有個(gè)問題,想問一下,為什么這些日志都是打印的兩行呢???
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
作者: whitelotus19    時(shí)間: 2014-10-04 11:03
回復(fù) 9# whyliyi


這倒真沒寫過 linux下的服務(wù)程序,回答不了這個(gè)問題 。
作者: whitelotus19    時(shí)間: 2014-10-04 11:11
whyliyi 發(fā)表于 2014-09-26 20:31
一下是從你demo里邊摘下來的日志,有個(gè)問題,想問一下,為什么這些日志都是打印的兩行呢???
2014 ...


啟動(dòng)了2個(gè)線程各記錄20條信息阿。




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2