0
import threading
這個功能我想要做我的編碼的東西爲每個線程,但只有mainthread進入線程的這個功能沒有已經在主函數創建
def enrich():
for i in range(10):
print(threading.current_thread().getName())
def main():
Thread_0 = threading.Thread(target=enrich())
Thread_1 = threading.Thread(target=enrich())
Thread_2 = threading.Thread(target=enrich())
Thread_3 = threading.Thread(target=enrich())
Thread_4 = threading.Thread(target=enrich())
Thread_5 = threading.Thread(target=enrich())
Thread_6 = threading.Thread(target=enrich())
Thread_7 = threading.Thread(target=enrich())
Thread_0.setName('0')
Thread_1.setName('1')
Thread_2.setName('2')
Thread_3.setName('3')
Thread_4.setName('4')
Thread_5.setName('5')
Thread_6.setName('6')
Thread_7.setName('7')
Thread_0.start()
Thread_1.start()
Thread_2.start()
Thread_3.start()
Thread_4.start()
Thread_5.start()
Thread_6.start()
Thread_7.start()
Thread_0.join()
Thread_1.join()
Thread_2.join()
Thread_3.join()
Thread_4.join()
Thread_5.join()
Thread_6.join()
Thread_7.join()
if __name__ == "__main__": main()
輸出我得到這樣的:
C:\Users\Danial\AppData\Local\Programs\Python\Python35\python.exe "D:/iaun/Final Project/FileTermsInfo/test.py"
MainThread
MainThread
MainThread
MainThread
MainThread
MainThread
MainThread
MainThread
非常感謝kindall我想確保我做萬物的權利,這就是爲什麼我時,我想通過一些變量作爲輸入函數沒有使用列表 – danitiger33
什麼情況呢? – danitiger33
這就是構造函數的'args'參數。或者在某些情況下,使用lambda或'functools.partial'可能會更方便。 – kindall