Is logging allowed outside of a function definition?
The question is whether it is OK to import a module that performs logging outside of a function definition, as in the following example:
# directly imported code (not within a function)
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.info('Yay!')
Some context is needed to understand my concerns about the above code. I have a simple module Module.py
that tests if import
is able to throw an exception when the module does not exist.
# Module.py
import threading
import logging
import sys
class Thread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global import_is_working
try:
sys.stderr.write("Yes, it got into the 'try' blockn")
import NON_EXISTENT_MODULE
assert False
except:
sys.stderr.write("Great, your python language is workingn")
import_is_working = True
Thread().start()
# The following statements will block if "example.log" is a FIFO.
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.info('Yay!')
Sometimes I like to import
the above code from another module:
# Main.py
import Module
At first, both modules seem to work:
max% python Module.py
Yes, it got into the 'try' block
Great, your python language is working
max% python Main.py
Yes, it got into the 'try' block
Great, your python language is working
max%
But sometimes I want to log to a pipe, and that can be blocking. The surprising thing is that this makes the two versions behave differently:
max% rm example.log
max% mkfifo example.log
max% python Module.py
Yes, it got into the 'try' block
Great, your python language is working
^C
max% python Main.py
Yes, it got into the 'try' block
^C
So I research this a bit, and, with some help from others, find the following string of words in the Python documentation:
... an import should not have the side effect of spawning a new thread and then waiting for that thread in any way.
These words, as I have cropped them above, appear to define a restriction which explains the problem with my program above.
The problem is that the string of words above actually is not the entire sentence in which we find those words within the Python documentation. In fact, the entire sentence is:
Other than in the main program, an import should not have the side effect of spawning a new thread and then waiting for that thread in any way.
The example above never imports spawning/waiting code other than in the main program, Main.py
. Therefore, the restriction does not apply at all!
Obviously the document is not providing all the information we need to decide what is safe. So this makes me wonder how incomplete the document is. After all, saying (emphasis mine)
Other than in the main program, an import should not have the side effect of spawning a new thread and then waiting for that thread in any way.
when the reality is
An import (anywhere) should not have the side effect of spawning a new thread and then waiting in any way.
is not so different from saying the same thing if the reality were actually
An import should not wait.
Logging can wait. Is it safe for imported code to perform logging?
If so, how can we trust it?
If not, how can we debug imported code that manipulates data? Or is it best to only do trivial initializations in imported code, ie, outside of a function definition?
Sorry to question the specification text, but if the manual is so terse, and provides no examples, and contains at least 1 error, then the questions are surely warranted.
链接地址: http://www.djcxy.com/p/87054.html上一篇: 更改shell中的文本颜色
下一篇: 日志允许在函数定义之外吗?