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

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
12下一頁(yè)
最近訪問(wèn)板塊 發(fā)新帖
查看: 7338 | 回復(fù): 11
打印 上一主題 下一主題

python多線程無(wú)法寫入日志 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2014-08-29 17:24 |只看該作者 |倒序?yàn)g覽
一個(gè)簡(jiǎn)單的測(cè)試,兩個(gè)線程,在線程中打印日志,日志打印到文件中,發(fā)現(xiàn)多線程無(wú)法打印日志。
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ù)制代碼
問(wèn)題:按理說(shuō)在t.py里邊打印的日志,應(yīng)該能夠打到hcagent.log里邊?可是為什么沒(méi)有呢?多線程需要做什么特殊處理?
懇請(qǐng)各位大神賜招

論壇徽章:
2
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:58:11
2 [報(bào)告]
發(fā)表于 2014-08-29 19:33 |只看該作者
據(jù)說(shuō)python多線程無(wú)法使用多core,與單線程性能一樣?樓主正好測(cè)試下,看網(wǎng)上傳言是否屬實(shí)。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2014-08-29 21:21 |只看該作者
本帖最后由 whitelotus19 于 2014-08-30 09:58 編輯

..........

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2014-08-30 08:46 |只看該作者
沒(méi)太看懂什么意思?能給點(diǎn)提示嗎?
我t.py也有自己的日志設(shè)置的呀

回復(fù) 3# whitelotus19


   

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2014-08-30 10:19 |只看該作者
我在3樓的回復(fù)沒(méi)注意看你的代碼,只是大概看了下你的描述,一下理解錯(cuò)了。

我沒(méi)有用過(guò)日志配置文件的方式,我這樣試了下好像可以的吧:

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è)意思?

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2014-08-30 14:39 |只看該作者
這樣可以了,但是有一個(gè)問(wèn)題,我把程序封裝成一個(gè)Windows服務(wù)程序后,又不能打印了。不知道為什么。
回復(fù) 5# whitelotus19


   

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2014-08-30 21:14 來(lái)自手機(jī) |只看該作者
我看了下以前寫的程序,應(yīng)該可以的阿,邏輯結(jié)構(gòu)大體跟你的程序相同,只是我是用的我前面貼的代碼的那種寫日志的方式,沒(méi)有用配置文件的方式。

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2014-08-31 15:25 |只看該作者
本帖最后由 whitelotus19 于 2015-03-14 16:19 編輯

我寫了個(gè)demo
http://lotus.pixub.com/?p=438
如果有不對(duì)的地方歡迎指正

論壇徽章:
0
9 [報(bào)告]
發(fā)表于 2014-09-18 12:32 |只看該作者
whitelotus19 發(fā)表于 2014-08-31 15:25
我寫了個(gè)demo
如果有不對(duì)的地方歡迎指正


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

還有個(gè)問(wèn)題,請(qǐng)問(wèn)你知道linux 的服務(wù)程序怎么寫嗎??我不太會(huì)

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2014-09-26 20:31 |只看該作者
whitelotus19 發(fā)表于 2014-08-31 15:25
我寫了個(gè)demo
如果有不對(duì)的地方歡迎指正



一下是從你demo里邊摘下來(lái)的日志,有個(gè)問(wèn)題,想問(wèn)一下,為什么這些日志都是打印的兩行呢???
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
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP