我将日志记录配置为
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s-%(levelname)s-%(process)d-%(processName)s- %(message)s')
logging.debug("Start of program")
def factorial(n):
logging.debug('Start of factorial(%s)' %(n))
total = 1
for i in range(n+1):
total *= i
logging.debug('i is ' + str(i) + ', toal is ' + str(total))
logging.debug('End of factorial(%s)' %(n))
return total
print(factorial(10))
logging.debug("End of program")
计划跟踪processName和processID,
不幸的是,它提供了一些有用的信息
$ python py_logging.py
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- Start of program
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- Start of factorial(10)
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- i is 0, toal is 0
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- i is 1, toal is 0
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- i is 2, toal is 0
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- i is 3, toal is 0
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- i is 4, toal is 0
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- i is 5, toal is 0
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- i is 6, toal is 0
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- i is 7, toal is 0
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- i is 8, toal is 0
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- i is 9, toal is 0
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- i is 10, toal is 0
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- End of factorial(10)
0
2019-03-20 23:11:15,014-DEBUG-12876-MainProcess- End of program
MainProcess是什么意思?不公开任何信息
我认为这样的结构可能会有所帮助
2019-03-20 23:18:47,447-DEBUG-16076-MainProcess- Start of program
systemd,1 splash
`-systemd,2122 --user
`-code,3521
`-code,3539
`-code,3691
`-bash,16025
`-python,16076 py_logging.py
`-sh,16077 -c pstree -asp 16076
`-pstree,16078 -asp 16076
2019-03-20 23:18:47,493-DEBUG-16076-MainProcess- Start of factorial(10)
2019-03-20 23:18:47,493-DEBUG-16076-MainProcess- i is 0, toal is 0
2019-03-20 23:18:47,493-DEBUG-16076-MainProcess- i is 1, toal is 0
2019-03-20 23:18:47,493-DEBUG-16076-MainProcess- i is 2, toal is 0
2019-03-20 23:18:47,493-DEBUG-16076-MainProcess- i is 3, toal is 0
2019-03-20 23:18:47,493-DEBUG-16076-MainProcess- i is 4, toal is 0
2019-03-20 23:18:47,493-DEBUG-16076-MainProcess- i is 5, toal is 0
2019-03-20 23:18:47,493-DEBUG-16076-MainProcess- i is 6, toal is 0
2019-03-20 23:18:47,493-DEBUG-16076-MainProcess- i is 7, toal is 0
2019-03-20 23:18:47,493-DEBUG-16076-MainProcess- i is 8, toal is 0
2019-03-20 23:18:47,493-DEBUG-16076-MainProcess- i is 9, toal is 0
2019-03-20 23:18:47,493-DEBUG-16076-MainProcess- i is 10, toal is 0
2019-03-20 23:18:47,494-DEBUG-16076-MainProcess- End of factorial(10)
0
2019-03-20 23:18:47,494-DEBUG-16076-MainProcess- End of program
我怎么理解MainProcess,主要是什么?
最佳答案
使用processName
检索multiprocessing.current_process().name
属性。这是主进程的"MainProcess"
(如果不使用multiprocessing
模块,则将拥有全部)。对于以multiprocessing
开头的子进程,可以在创建Process
对象时指定新进程的名称,默认情况下它将是“ Process”,后跟一些数字,具体取决于您产生了多少个子进程。
关于python - MainProcess以日志记录格式%process,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55264328/