我創建了incl.py和ml.py。 incl.py將從幾個目錄中的一個目錄加載,每個目錄都包含這樣一個文件incl.py。 ml.py是incl.py通過read()
和exec()
的「主」加載。 每個incl.py預計包含一組具有相同名稱和接口但可能具有不同行爲的函數。線程exec-utes成功的另一個python文件,但不知道包含的功能
ml.py啓動一個或多個線程。 每個線程應該從其個人目錄加載incl.py。 加載工作正常,但是,加載的函數似乎不知道該線程。
內容incl.py:
內容的def printIncluded (parameter):
print (parameter)
ml.py:
import threading
def threadContent (parameter):
exec (open ("incl.py").read())
printIncluded (parameter)
thread = threading.Thread (target = threadContent, args = (("loaded from thread"),))
thread.start()
只要我不使用線程它的作品,例如用以下內容ml.py:
exec (open ("incl.py").read())
printIncluded ("directly loaded")
當在線程中工作時,需要考慮什麼exec()
?